2

我正在使用托管 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
    }
}
4

2 回答 2

3

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

        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 上测试。

于 2013-11-14T18:37:43.990 回答
0

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

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

在 WlanApi.cs 中更改为: public IntPtr clientHandle; (您需要 clientHandle。不知道为什么它被设置为私有?)

使用此代码:

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);
            }

祝你好运 :)

于 2020-07-03T18:13:40.930 回答