使用托管WiFi(NativeWiFi API)的问题 [英] Issues with using Managed WiFi (NativeWiFi API)
问题描述
我正在尝试使用本机WiFi创建并连接到WLAN配置文件( https://managedwifi.codeplex.com /).我能够查看所有的网络BSS列表及其参数.但是,当我尝试创建/覆盖WLAN配置文件时,出现以下错误消息(Error#1):
I am trying to create and connect to a WLAN profile using Native WiFi (https://managedwifi.codeplex.com/). I am able to view all the Network BSS List and their parameters. However, when I am trying to create/overwrite a WLAN profile, I get the below mentioned error message (Error#1):
ManagedWifi.dll中发生了类型为'System.ComponentModel.Win32Exception'的未处理的异常.
An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in ManagedWifi.dll.
其他信息:网络连接配置文件已损坏
Additional information: The network connection profile is corrupted
但是,当我通常从Windows 7控制面板的网络和共享中心"创建配置文件,然后尝试使用ManagedWiFi进行连接时,出现了另一条错误消息(错误#2):
However, when I created a profile normally from "Network and Sharing Center" of the Windows 7 control panel and then tried to connect using the ManagedWiFi, I get another error message(Error#2):
mscorlib.dll中发生了'System.ArgumentException'类型的未处理异常
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
其他信息:不能将"NativeWifi.Wlan + WlanReasonCode"类型编组为非托管结构;无法计算出有意义的尺寸或偏移量.
Additional information: Type 'NativeWifi.Wlan+WlanReasonCode' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
即使在Windows应用程序在后台运行的情况下,即使尝试从网络和共享中心"连接/断开与WLAN配置文件的连接,我也会注意到此错误.
I noticed that this error occurs even if I try to connect/disconnect to a WLAN profile from the "Network and Sharing Center", with the windows application running in the background.
这是我正在使用的示例代码:
Here is the sample code that I am using:
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
从论坛"类型Wifi.Wlan + WlanReasonCode无法解决错误,问题(Error#2)似乎在WlanAPI.cs中,其中有一行代码检查返回代码的大小.这是一行:
From the forum "Type Native Wifi.Wlan + WlanReasonCode cannot be marshaled error", the issue (Error#2) seems to be within the WlanAPI.cs, where there is a line of code that checks for the size of the return code. This is the line:
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);
}
将上面的代码更改为下面的代码似乎可以解决该问题.
Changing the above code to the below seems to fix the issue.
//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文件.对于上述两个问题的任何帮助,将不胜感激.
However, I am not sure on how to add this fix to my solution. I installed the ManagedWiFi from the NuGet Package Manager. Hence, not sure how to change the WlanApi.cs file. Any help regarding the above mentioned two issues are much appreciated.
推荐答案
问题(Error#1)现在已解决. profilexml文件格式对我来说是不同的.这是我更改后的profilexml.
The issue (Error#1) is now resolved. The profilexml file format was different for me. Here is the profilexml after I changed it.
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)也已解决.然后,如 SimpleWiFi 或输入原生Wifi .Wlan + WlanReasonCode不能编组错误.
Also the second issue (Error#2) was resolved when I uninstalled ManagedWiFi package from my solution and added the whole ManagedWiFi project to the solution. Then I made the change in WlanApi.cs as mentioned in SimpleWiFi Or Type Native Wifi.Wlan + WlanReasonCode cannot be marshaled error.
这篇关于使用托管WiFi(NativeWiFi API)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!