类型Native Wifi.Wlan + WlanReasonCode无法封送错误 [英] Type Native Wifi.Wlan + WlanReasonCode cannot be marshaled error

查看:168
本文介绍了类型Native Wifi.Wlan + WlanReasonCode无法封送错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想就我在运行Wifi配置文件添加代码时遇到的此错误询问您的意见.

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

现在您所看到的代码是我正在使用的示例(因为我是C#中Wlan接口的新手).

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

我正在尝试向程序中添加无线功能,该功能将搜索某个无线网络,将该配置文件添加到PC并将其连接(ping测试-可以正常工作).

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

请查看错误图片:此处 我发现的是,如果我从Windows文件夹中删除Cheesecake配置文件,则不会发生该错误.这告诉我该错误可能是由于配置文件已被存储所致.但不确定.

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.

我正在使用Managed Wifi API,并且在WlanAPI.cs中,有一行代码检查返回代码的大小.这是一行:

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

我确实发现,当原因码为成功"时,其枚举值为0.因此,我注释了该行,并在下一行中添加了零,如下所示:

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

//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?

谢谢, 垫子

这篇关于类型Native Wifi.Wlan + WlanReasonCode无法封送错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆