2017-02-21 507 views
2

我嘗試使用本機WiFi(https://managedwifi.codeplex.com/)創建並連接到WLAN配置文件。我可以查看所有網絡BSS列表及其參數。然而,當我試圖創建/覆蓋WLAN的配置文件,我得到下面提到的錯誤信息(錯誤#1):使用託管WiFi(NativeWiFi API)的問題

型「System.ComponentModel.Win32Exception」未處理的異常發生在ManagedWifi.dll 。

附加信息:網絡連接配置文件損壞

然而,當我從「網絡和共享中心」 Windows 7的控制面板中創建一個配置文件正常,然後試着用ManagedWiFi連接時,我得到另一個錯誤信息(錯誤#2):

類型 'System.ArgumentException' 的未處理的異常出現在mscorlib.dll

其他信息:類型「NativeWifi.Wlan + WLA nReasonCode'不能作爲非託管結構編組;無法計算出有意義的大小或偏移量。

我注意到,即使我嘗試從「網絡和共享中心」連接/斷開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內,檢查返回碼大小的代碼。這是行:

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 Package Manager安裝了ManagedWiFi。因此,不知道如何更改WlanApi.cs文件。有關上述兩個問題的任何幫助都非常感謝。

+0

奇怪的是,這種改變不應該影響任何東西。通過查看它可以說它增加了異常的風險,因爲代碼的作用是驗證'notifyData'的大小至少與'Wlan.WlanReasonCode'大小相同(如果這是一個整數枚舉,大小應該是4,如4字節/ int)。 –

+0

由於我從我的解決方案中卸載了ManagedWiFi軟件包並將整個ManagedWiFi項目添加到解決方案,因此錯誤#2似乎已得到修復。然後我在上面的修復中提到了WlanApi.cs中的更改。 –

+0

好吧,這是積極的,那麼NuGet包肯定有問題,但構建源代碼仍然有效。 –

回答

3

問題(錯誤#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)得到解決。然後我在SimpleWiFiType Native Wifi.Wlan + WlanReasonCode cannot be marshaled error中提到了WlanApi.cs中的更改。

+0

感謝您的錯誤#2修復,點上 – NSGaga