如何打开实际关闭的Wifi适配器的无线电?

时间:2013-11-14 18:37:44

标签: c# .net interop wifi

我正在使用Managed Wifi来获取我的Wifi适配器的无线电状态。 如果实际关闭,如何打开收音机?

这样的事情:

WlanClient wlanClient = new WlanClient()
var targetInterface = wlanClient.Interfaces.FirstOrDefault()
if (targetInterface != null)
{
    bool radioIsOff = targetInterface .RadioState.PhyRadioState[0].dot11SoftwareRadioState == Wlan.Dot11RadioState.Off;
    if (radioIsOff)
    {
       // TODO
    }
}

2 个答案:

答案 0 :(得分:3)

我刚将其添加到托管Wifi API的WlanInterface类:

        IntPtr radioStatePtr = new IntPtr(0L);
        try
        {
            Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
            radioState.dwPhyIndex = 0; // TODO : can change ???
            radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On; // ignored in fact, according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms706791(v=vs.85).aspx 
            radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.On;

            radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
            Marshal.StructureToPtr(radioState, radioStatePtr, false);

            Wlan.ThrowIfError(
                Wlan.WlanSetInterface(
                            client.clientHandle,
                            info.interfaceGuid,
                            Wlan.WlanIntfOpcode.RadioState,
                            (uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)),
                            radioStatePtr,
                            IntPtr.Zero));
        }
        finally
        {
            if (radioStatePtr.ToInt64() != 0)
                Marshal.FreeHGlobal(radioStatePtr);
        }

在Win 7上测试。

答案 1 :(得分:0)

我为此感到挣扎,我只想分享我的解决方案

(上面建议下载托管wifi) 将WlanApi.cs和Interop.cs添加到您的项目。 使用NativeWifi添加。

在WlanApi.cs中 改成: 公共IntPtr clientHandle; (您需要clientHandle。不确定为什么将其设置为private?)

使用此代码:

string arg1 = "true"; //set to false if you want to turn it off.
      arg1 = arg1.ToLower();
            IntPtr radioStatePtr = new IntPtr(0L);
            try
            {
                WlanClient wc = new WlanClient();

                
                foreach (var iface in wc.Interfaces)
                {
                    //WlanInterface
                    if(iface.InterfaceName.ToLower()=="wifi")
                    { 
                    Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
                    radioState.dwPhyIndex = 0;
                    if(arg1=="true")
                    { 
                        radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On;
                        radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.On;
                    }
                    else
                    {
                        radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.Off;
                        radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.Off;
                    }
                    radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
                    Marshal.StructureToPtr(radioState, radioStatePtr, false);

                    Wlan.WlanSetInterface(wc.clientHandle, iface.InterfaceGuid, Wlan.WlanIntfOpcode.RadioState, (uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)), radioStatePtr, IntPtr.Zero);
                    }

                }
               
            }
            finally
            {
                if (radioStatePtr.ToInt64() != 0)
                    Marshal.FreeHGlobal(radioStatePtr);
            }

祝你好运:)