C#打开WiFi热点,WiFi共享的四种方式总结

Posted 拚忘vie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于C#打开WiFi热点,WiFi共享的四种方式总结的主要内容,如果未能解决你的问题,请参考以下文章

android怎么开启wifi热点

如何用android手机当wifi热点

mac笔记本电脑开启wifi热点

安卓手机怎样用电脑连接wifi上网?

关于 win10 创建WiFi热点 问题

android 怎么启动wifi热点