Wifi.Wlan + WlanReasonCode不能作为非托管结构进行封送处理,无法计算有意义的大小或偏移量 - 小众知识

Wifi.Wlan + WlanReasonCode不能作为非托管结构进行封送处理,无法计算有意义的大小或偏移量

2013年01月27日 14:18:05 苏内容
  标签: Wifi/Wlan
阅读:7328

Wanted to ask your opinion on this error that I am getting while running my Wifi profile add code.

Now the code that you are seeing is an example which I am using (coz I am new to the Wlan interface in C#).

I am trying to add wlan capability to my program that will search for a certain wireless network, add that profile to the PC and connect it (ping test - got this working yay).

Please see the pic for the error: here What I have found is that if I remove the Cheesecake profile from the windows folder, the error does not happen. This tells me that the error could be something due to the profile being stored already. But not sure about it.

I am attaching my example code here as well FYI. Any help would be appreciated.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using NativeWifi;

    namespace WIFI_CONTROL_EXAMPLE
    {
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        lstNetworks.Items.Clear();
        WlanClient client = new WlanClient();
        foreach(WlanClient.WlanInterface wlanIface in client.Interfaces )
        {
            Wlan.WlanAvailableNetwork[]networks = wlanIface.GetAvailableNetworkList(0);
            foreach (Wlan.WlanAvailableNetwork network in networks)
            {
                Wlan.Dot11Ssid ssid = network.dot11Ssid;
                string networkName = Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);

                ListViewItem item = new ListViewItem(networkName);
                item.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString());
                item.SubItems.Add(network.wlanSignalQuality + "%");
                lstNetworks.Items.Add(item);

            }


        }


    }

    private void button2_Click(object sender, EventArgs e)
    {
        WlanClient client = new WlanClient();
        foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
        {

            string profileName = "Cheesecake"; // this is also the SSID
            string mac = "52544131303235572D454137443638";
            string key = "hello";
            string profileXml = 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>", profileName, mac, key);
            wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
            wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
        }



        //string x = "";

        //x = lstNetworks.SelectedItems[0].Text;
        //foreach
        //MessageBox.Show("you have selected " + x);
        //string profileName = x;
        //string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID><nonBroadcast>false</nonBroadcast></SSIDConfig><connectionType>ESS</connectionType><connectionMode>manual</connectionMode><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>", profileName);
        //wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
        //wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);

    }
}
   }

I think I may have fixed the problem, but Im not sure what will be the effects of it.

I am using Managed Wifi API and within the WlanAPI.cs, 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));

what I did find out was that when the Reason code is "Success", it has an enumerated value of 0. So I commented the line and added zero in the next line as such:

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

to

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

this seems to work, but Im not sure if I opened a can of worms. Can you guys think of any other better solution?

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.


扩展阅读