输入Native Wifi.Wlan + Wlan reasoncode无法封送错误

本文关键字:Wlan 错误 reasoncode Native Wifi 输入 | 更新日期: 2023-09-27 18:04:31

想问问你对这个错误的看法,我在运行我的Wifi配置文件添加代码时得到。

现在你看到的代码是我正在使用的一个例子(因为我是c#中Wlan接口的新手)。

我正试图将wlan功能添加到我的程序中,该程序将搜索某个无线网络,将该配置文件添加到PC并连接它(ping测试-得到了这个工作yay)。

请参阅pic的错误:在这里我发现的是,如果我从windows文件夹中删除Cheesecake配置文件,则不会发生错误。这告诉我错误可能是由于配置文件已经存储。但我不确定。

我在这里附上我的示例代码以及供参考。如有任何帮助,不胜感激。

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

谢谢,垫

输入Native Wifi.Wlan + Wlan reasoncode无法封送错误

我想我可能已经解决了这个问题,但是我不确定它会产生什么影响。

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

 int expectedSize = Marshal.SizeOf(typeof(Wlan.WlanReasonCode));

我所发现的是,当Reason代码是"Success"时,它的枚举值为0。所以我注释了这行,并在下一行添加了0,如下所示:

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

这似乎工作,但我不确定如果我打开了一个蠕虫罐头。你们能想到其他更好的解决办法吗?

谢谢,垫