Issues with using Managed WiFi (NativeWiFi API)
Asked Answered
P

2

3

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):

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

Additional information: The network connection profile is corrupted

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):

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.

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

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

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.

Pazpaza answered 21/2, 2017 at 15:47 Comment(7)
The strange thing is that that change should not affect anything. By looking at it one would say it increases the risk of exceptions, since what that code does is that it verifies that the size of notifyData is at least the same size as Wlan.WlanReasonCode (if this is an integer enumeration, the size should be 4 as in 4 bytes/int).Bulley
Error#2 seems to be fixed as 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 the fix above.Pazpaza
Well that's positive, then there must've been a problem with the NuGet package but building the source code still works.Bulley
I think the NuGet package was an old version. Error #2 seems to have been solved in one of the later versions: managedwifi.codeplex.com/workitem/14284Bulley
Thanks. I actually downloaded it from the Source Code page than the Downloads page as mentioned in the comments section. Also changing as per SimpleWiFi also helps.Pazpaza
Found that Error#1 occurs at the function public Wlan.WlanReasonCode SetProfile(Wlan.WlanProfileFlags flags, string profileXml, bool overwrite) { Wlan.WlanReasonCode reasonCode; Wlan.ThrowIfError( Wlan.WlanSetProfile(client.clientHandle, info.interfaceGuid, flags, profileXml, null, overwrite, IntPtr.Zero, out reasonCode)); return reasonCode; } of WlanApi.cs. The error occurs at Wlan.ThrowIfError(). Don't know how to fix this issue.Pazpaza
I think it's related to what is passed to the native function. It should return ERROR_BAD_PROFILE and a reason in the pdwReasonCode parameter if there's something wrong with the profile. It seems however that ManagedWifi does not read this and only throws "profile is corrupt" instead (though I don't know for certain since I'm currently on my phone and don't have the source code in front of me).Bulley
P
3

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)

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.

Pazpaza answered 22/2, 2017 at 12:52 Comment(0)
B
0

I had a simpler task (read the SSID of the connected network), which was throwing the same error.

I solved it by switching to using SimpleWiFi entirely and ignore the ManagedWifi package.

Glancing at the source code, it looks like SW is a fixed reimplementation of some of the functionality in MW.

Barghest answered 7/2, 2020 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.