使用托管 WiFi 的问题 (NativeWiFi API)

Issues with using Managed WiFi (NativeWiFi API)

我正在尝试使用本地 WiFi (https://managedwifi.codeplex.com/) 创建并连接到 WLAN 配置文件。我能够查看所有网络 BSS 列表及其参数。但是,当我尝试 create/overwrite WLAN 配置文件时,我收到下面提到的错误消息 (Error#1):

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in ManagedWifi.dll.

Additional information: The network connection profile is corrupted

但是,当我通常从 Windows 7 控制面板的 "Network and Sharing Center" 创建配置文件,然后尝试使用 ManagedWiFi 连接时,我收到另一条错误消息(错误#2):

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Type 'NativeWifi.Wlan+WlanReasonCode' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.

我注意到,即使我尝试从 "Network and Sharing Center" connect/disconnect 到 WLAN 配置文件,后台有 windows 应用程序 运行,也会发生此错误。

这是我使用的示例代码:

Dim profileName As String = GlobalVariables.ssidname          ' Provides the selected SSID name from the Network BSS List 
Dim hexval As String = StringToHex(GlobalVariables.ssidname)  ' Function to get the hexadecimal value for a provided string
Dim key As String = TextBox1.Text                             ' Security key from the textbook provided

Dim profileXml As String = String.Format("<?xml version=""1.0""?><WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1""><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", 'GlobalVariables.ssidname, hexval, TextBox1.Text)            
wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, True)  'Error#1 occurs here
wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName)   'Error#2 occurs here

来自论坛“Type Native Wifi.Wlan + WlanReasonCode cannot be marshaled error”,问题(错误#2)似乎在 WlanAPI.cs 内,其中有一行代码检查 return代码。这是行:

int expectedSize = Marshal.SizeOf(typeof(Wlan.WlanReasonCode));
if (notifyData.dataSize >= expectedSize)
{
    Wlan.WlanReasonCode reasonCode = (Wlan.WlanReasonCode)Marshal.ReadInt32(notifyData.dataPtr);
    if (wlanIface != null)
        wlanIface.OnWlanReason(notifyData, reasonCode);
}

将上面的代码更改为下面的代码似乎可以解决问题。

//int expectedSize = Marshal.SizeOf(typeof(Wlan.WlanReasonCode));
if (notifyData.dataSize >= 0)
{
    Wlan.WlanReasonCode reasonCode = (Wlan.WlanReasonCode)Marshal.ReadInt32(notifyData.dataPtr);
    if (wlanIface != null)
        wlanIface.OnWlanReason(notifyData, reasonCode);
}

但是,我不确定如何将此修复程序添加到我的解决方案中。我从 NuGet 包管理器安装了 ManagedWiFi。因此,不确定如何更改 WlanApi.cs 文件。非常感谢有关上述两个问题的任何帮助。

问题(错误#1)现已解决。 profilexml 文件格式对我来说不同。这是我修改后的profilexml。

Dim profileXml As String = String.Format("<?xml version=""1.0""?><WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1""><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><MSM><security><authEncryption><authentication>WPA2PSK</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey></security></MSM></WLANProfile>", GlobalVariables.ssidname, hexval, TextBox1.Text)

当我从解决方案中卸载 ManagedWiFi 包并将整个 ManagedWiFi 项目添加到解决方案时,第二个问题(错误 #2)也得到了解决。然后我在 WlanApi.cs 中进行了更改,如 SimpleWiFi Or Type Native Wifi.Wlan + WlanReasonCode cannot be marshaled error.

中所述

我有一个更简单的任务(读取连接网络的 SSID),它抛出了同样的错误。

我通过完全切换到使用 SimpleWiFi 并忽略 ManagedWifi 包解决了这个问题。

看了一下源代码,SW 似乎是对 MW 中某些功能的固定重新实现。