1. 程式人生 > 其它 >C#開啟WiFi熱點,WiFi共享的四種方式總結(二)

C#開啟WiFi熱點,WiFi共享的四種方式總結(二)

拚忘的部落格園要開始寫隨筆了,第一期就先講講wifi熱點吧.....

持續更新C#、wpf、.NetCore相關內容,歡迎關注!!

C#開啟WiFi熱點,WiFi共享的四種方式總結(一)

C#開啟WiFi熱點,WiFi共享的四種方式總結(二)

C#開啟WiFi熱點,WiFi共享的四種方式總結(三)

C#開啟WiFi熱點,WiFi共享的四種方式總結(四)

C# Windows系統 ,WiFi共享的方式,開啟WiFi熱點我最近研究總結了又四種方式開啟。

1.通過netsh wlan命令列開啟

2.通過wlanapi介面共享

3.通過Win10 Api開啟,Windows.Devices.WiFiDirect

4.通過Win10 api,Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager開啟電腦自帶的熱點。

二、通過windows提供的wlanapi.dll來開啟熱點共享。

微軟也是提供了操作netsh的相關介面,其實原理和通過Cmd操作netsh wlan是一樣的同樣也需要網絡卡支援承載,一般Win10現在很多網絡卡不支援。

通過CMD的方式參考上文:https://www.cnblogs.com/panwangvie/p/14878148.html

具體API參考文件:http://www.pinvoke.net/default.aspx/wlanapi.wlanconnect

需要用到的api如下:

public static class WlanNativeMethods
    {
        public delegate void WlanNotificationCallback(ref WlanNotificationData notificationData, IntPtr context);

        /// <summary>
        /// </summary>
        /// <param name="hClientHandle"></param>
        /// <param name="pReserved">
Must pass in IntPtr.Zero</param> /// <returns></returns> [DllImport("Wlanapi.dll", EntryPoint = "WlanCloseHandle")] public static extern int WlanCloseHandle([In] IntPtr hClientHandle, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkForceStart")] public static extern int WlanHostedNetworkForceStart(IntPtr hClientHandle, [Out] out WlanHostedNetworkReason pFailReason, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkForceStop")] public static extern int WlanHostedNetworkForceStop(IntPtr hClientHandle, [Out] out WlanHostedNetworkReason pFailReason, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkInitSettings")] public static extern int WlanHostedNetworkInitSettings(IntPtr hClientHandle, [Out] out WlanHostedNetworkReason pFailReason, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkQueryProperty")] public static extern int WlanHostedNetworkQueryProperty(IntPtr hClientHandle, WlanHostedNetworkOpcode opCode, [Out] out uint pDataSize, [Out] out IntPtr ppvData, [Out] out WlanOpcodeValueType pWlanOpcodeValueType, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkQuerySecondaryKey")] public static extern int WlanHostedNetworkQuerySecondaryKey(IntPtr hClientHandle, [Out] out uint pKeyLength, [Out, MarshalAs(UnmanagedType.LPStr)] out string ppucKeyData, [Out] out bool pbIsPassPhrase, [Out] out bool pbPersistent, [Out] out WlanHostedNetworkReason pFailReason, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkQueryStatus")] public static extern int WlanHostedNetworkQueryStatus(IntPtr hClientHandle, [Out] out IntPtr ppWlanHostedNetworkStatus, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkRefreshSecuritySettings")] public static extern int WlanHostedNetworkRefreshSecuritySettings(IntPtr hClientHandle, [Out] out WlanHostedNetworkReason pFailReason, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkSetProperty")] public static extern int WlanHostedNetworkSetProperty(IntPtr hClientHandle, WlanHostedNetworkOpcode opCode, uint dwDataSize, IntPtr pvData, [Out] out WlanHostedNetworkReason pFailReason, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkSetSecondaryKey")] public static extern int WlanHostedNetworkSetSecondaryKey(IntPtr hClientHandle, uint dwKeyLength, byte[] pucKeyData, bool bIsPassPhrase, bool bPersistent, [Out] out WlanHostedNetworkReason pFailReason, IntPtr pReserved); [DllImport("Wlanapi.dll", EntryPoint = "WlanOpenHandle")] public static extern int WlanOpenHandle(uint dwClientVersion, IntPtr pReserved, [Out] out uint pdwNegotiatedVersion, [Out] out IntPtr clientHandle); [DllImport("Wlanapi.dll", EntryPoint = "WlanRegisterNotification")] public static extern int WlanRegisterNotification(IntPtr hClientHandle, WlanNotificationSource dwNotifSource, bool bIgnoreDuplicate, WlanNotificationCallback funcCallback, IntPtr pCallbackContext, IntPtr pReserved, [Out] out WlanNotificationSource pdwPrevNotifSource); }
View Code

啟動流程:

private static void Start()
        {
            Close();
            Try(() =>
            {
                WlanHostedNetworkReason reason;
                Console.Write("啟用託管網路中……");
                reason = WlanManager.SetEnabled(true);
                if (reason != WlanHostedNetworkReason.Success)
                {
                    Close();
                    return;
                }
                Console.Write("正在應用設定……");
                reason = WlanManager.SetConnectionSettings(ssid, peersCount);
                if (reason != WlanHostedNetworkReason.Success)
                {
                    WriteReason(reason);
                    Close();
                    return;
                }
                var keyString = key as string;
                reason = keyString != null ? WlanManager.SetSecondaryKey(keyString)
                                                       : WlanManager.SetSecondaryKey((byte[]) key);
                if (reason != WlanHostedNetworkReason.Success)
                {
                    Close();
                    return;
                }
                Console.Write("啟動共享中……");
                 WlanManager.ForceStart() ;
            }, Close);
        }
  public static WlanHostedNetworkReason SetConnectionSettings(string hostedNetworkSsid, int maxNumberOfPeers)
        {
            WlanHostedNetworkReason failReason;
            var settings = new WlanHostedNetworkConnectionSettings
                { HostedNetworkSSID = ToDOT11_SSID(hostedNetworkSsid), MaxNumberOfPeers = (uint)maxNumberOfPeers };
            var settingsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(settings));
            Marshal.StructureToPtr(settings, settingsPtr, false);
             WlanNativeMethods.WlanHostedNetworkSetProperty(wlanHandle,
                                        WlanHostedNetworkOpcode.ConnectionSettings,
                                        (uint)Marshal.SizeOf(settings), settingsPtr, out failReason, IntPtr.Zero);
            return failReason;
        }

        public static WlanHostedNetworkReason SetEnabled(bool enabled)
        {
            WlanHostedNetworkReason failReason;
            var settingsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(enabled));
            Marshal.StructureToPtr(enabled, settingsPtr, false);
           WlanNativeMethods.WlanHostedNetworkSetProperty(wlanHandle,
                                        WlanHostedNetworkOpcode.Enable,
                                        (uint)Marshal.SizeOf(enabled), settingsPtr, out failReason, IntPtr.Zero);
            return failReason;
        }

      public static WlanHostedNetworkReason SetSecondaryKey(string passKey, bool isPersistent = true)
        {
            WlanHostedNetworkReason failReason;
             WlanNativeMethods.WlanHostedNetworkSetSecondaryKey(wlanHandle,
                (uint)(passKey.Length + 1), Encoding.Default.GetBytes(passKey), true, isPersistent, out failReason,
                IntPtr.Zero);
            return failReason;
        }
    public static WlanHostedNetworkReason SetSecondaryKey(byte[] passKey, bool isPersistent = true)
        {
            WlanHostedNetworkReason failReason;
             WlanNativeMethods.WlanHostedNetworkSetSecondaryKey(wlanHandle,
                                       32, passKey, false, isPersistent, out failReason, IntPtr.Zero);
            return failReason;
        }
 public static WlanHostedNetworkReason ForceStart()
        {
            WlanHostedNetworkReason failReason;
           WlanNativeMethods.WlanHostedNetworkForceStart(wlanHandle, out failReason, IntPtr.Zero);
            return failReason;
        }

        public static WlanHostedNetworkReason ForceStop()
        {
            WlanHostedNetworkReason failReason;
            WlanNativeMethods.WlanHostedNetworkForceStop(wlanHandle, out failReason, IntPtr.Zero);
            return failReason;
        }
View Code

程式碼下載:https://gitee.com/panwangvie