diff --git a/SimpleWifi/AccessPoint.cs b/SimpleWifi/AccessPoint.cs index c1d10ae..e660e7d 100644 --- a/SimpleWifi/AccessPoint.cs +++ b/SimpleWifi/AccessPoint.cs @@ -25,7 +25,7 @@ public string Name { get { - return Encoding.ASCII.GetString(_network.dot11Ssid.SSID, 0, (int)_network.dot11Ssid.SSIDLength); + return Encoding.UTF8.GetString(_network.dot11Ssid.SSID, 0, (int)_network.dot11Ssid.SSIDLength); } } @@ -70,7 +70,7 @@ public bool IsConnected try { var a = _interface.CurrentConnection; // This prop throws exception if not connected, which forces me to this try catch. Refactor plix. - return a.profileName == _network.profileName; + return a.profileName == _network.profileName && a.isState == WlanInterfaceState.Connected; } catch { diff --git a/SimpleWifi/ProfileFactory.cs b/SimpleWifi/ProfileFactory.cs index 8b78821..ef84caa 100644 --- a/SimpleWifi/ProfileFactory.cs +++ b/SimpleWifi/ProfileFactory.cs @@ -21,6 +21,8 @@ internal static string Generate(WlanAvailableNetwork network, string password) string name = Encoding.ASCII.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength); string hex = GetHexString(network.dot11Ssid.SSID); + password = System.Security.SecurityElement.Escape(password); + var authAlgo = network.dot11DefaultAuthAlgorithm; switch (network.dot11DefaultCipherAlgorithm) diff --git a/SimpleWifi/Win32/Interop/Enums.cs b/SimpleWifi/Win32/Interop/Enums.cs index 4e52f1b..c4ac084 100644 --- a/SimpleWifi/Win32/Interop/Enums.cs +++ b/SimpleWifi/Win32/Interop/Enums.cs @@ -823,14 +823,27 @@ public enum Dot11OperationMode : uint NetworkMonitor = 0x80000000 } - - /// - /// A set of flags that modify the behavior of the function: WlanSetProfileEapUserData - /// - /// On Windows Vista and Windows Server 2008, this parameter is reserved and should be set to zero. - /// On Windows 7, Windows Server 2008 R2, and later, this parameter can be one of the following values. - /// - [Flags] + /// + /// Defines the radio state of a wireless connection. + /// + /// + /// Corresponds to the native DOT11_RADIO_STATE enumeration. + /// + public enum Dot11RadioState : uint + { + Unknown = 0, + On, + Off + } + + + /// + /// A set of flags that modify the behavior of the function: WlanSetProfileEapUserData + /// + /// On Windows Vista and Windows Server 2008, this parameter is reserved and should be set to zero. + /// On Windows 7, Windows Server 2008 R2, and later, this parameter can be one of the following values. + /// + [Flags] public enum SetEapUserDataMode : uint { None = 0x00000000, diff --git a/SimpleWifi/Win32/Interop/Structs.cs b/SimpleWifi/Win32/Interop/Structs.cs index cc10b29..30985e8 100644 --- a/SimpleWifi/Win32/Interop/Structs.cs +++ b/SimpleWifi/Win32/Interop/Structs.cs @@ -588,7 +588,45 @@ public struct WlanProfileInfo public WlanProfileFlags profileFlags; } - /* + /// + /// Defines the radio state attributes for a wireless connection. + /// + /// + /// Corresponds to the native WLAN_PHY_RADIO_STATE structure. + /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public struct WlanPhyRadioState + { + public int dwPhyIndex; + public Dot11RadioState dot11SoftwareRadioState; + public Dot11RadioState dot11HardwareRadioState; + } + + /// + /// Defines the radio state attributes for a wireless connection. + /// + /// + /// Corresponds to the native WLAN_RADIO_STATE type. + /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public struct WlanRadioState + { + public int numberofItems; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] + private WlanPhyRadioState[] phyRadioState; + public WlanPhyRadioState[] PhyRadioState + { + get + { + WlanPhyRadioState[] ret = new WlanPhyRadioState[numberofItems]; + Array.Copy(phyRadioState, ret, numberofItems); + return ret; + } + } + } + + /* /// /// The EAP_METHOD_TYPE structure contains type, identification, and author information about an EAP method. /// diff --git a/SimpleWifi/Win32/WlanInterface.cs b/SimpleWifi/Win32/WlanInterface.cs index 432228c..3e3daba 100644 --- a/SimpleWifi/Win32/WlanInterface.cs +++ b/SimpleWifi/Win32/WlanInterface.cs @@ -633,5 +633,71 @@ private int GetInterfaceInt(WlanIntfOpcode opCode) WlanInterop.WlanFreeMemory(valuePtr); } } - } -} + + /// + /// Turn on WiFi. + /// + public void TurnOnWiFi() + { + IntPtr radioStatePtr = IntPtr.Zero; + + try + { + WlanPhyRadioState radioState = new WlanPhyRadioState + { + dwPhyIndex = 0, + dot11HardwareRadioState = Dot11RadioState.On, + dot11SoftwareRadioState = Dot11RadioState.On + }; + + radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState)); + Marshal.StructureToPtr(radioState, radioStatePtr, false); + + WlanInterop.ThrowIfError(WlanInterop.WlanSetInterface( + client.clientHandle, + info.interfaceGuid, + WlanIntfOpcode.RadioState, + (uint)Marshal.SizeOf(typeof(WlanPhyRadioState)), + radioStatePtr, + IntPtr.Zero)); + } + finally + { + Marshal.FreeHGlobal(radioStatePtr); + } + } + + /// + /// Turn off WiFi. + /// + public void TurnOffWiFi() + { + IntPtr radioStatePtr = IntPtr.Zero; + + try + { + WlanPhyRadioState radioState = new WlanPhyRadioState + { + dwPhyIndex = 0, + dot11HardwareRadioState = Dot11RadioState.Off, + dot11SoftwareRadioState = Dot11RadioState.Off + }; + + radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState)); + Marshal.StructureToPtr(radioState, radioStatePtr, false); + + WlanInterop.ThrowIfError(WlanInterop.WlanSetInterface( + client.clientHandle, + info.interfaceGuid, + WlanIntfOpcode.RadioState, + (uint)Marshal.SizeOf(typeof(WlanPhyRadioState)), + radioStatePtr, + IntPtr.Zero)); + } + finally + { + Marshal.FreeHGlobal(radioStatePtr); + } + } + } +} \ No newline at end of file