使用本机WiFi API访问无线网络参数 [英] Accessing wireless network parameters using native WiFi API

查看:158
本文介绍了使用本机WiFi API访问无线网络参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处中,我已经尝试了此代码.成功构建但输出
无线网络适配器尚未准备好运行.按任意键继续..."

i have tried this code given here. It builds succesfully but outputs
"wireless network adapter not ready to operate. Press any key to continue ......"

推荐答案



WLAN_INTERFACE_INFO结构布局定义中存在一个小错误.我已经解决了这个问题.您需要更新以下两个结构定义以解决未准备就绪"状态消息:

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
公共结构WLAN_INTERFACE_INFO
{
///GUID-& gt; _GUID
公共Guid接口向导;

///WCHAR [256]
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 256)]
公共字符串strInterfaceDescription;

///WLAN_INTERFACE_STATE-& gt; _WLAN_INTERFACE_STATE
公共WLAN_INTERFACE_STATE isState;
}
///& lt; summary& gt;
///此结构包含一组NIC信息
///& lt;/summary& gt;
[StructLayout(LayoutKind.Sequential)]
公共结构WLAN_INTERFACE_INFO_LIST
{
公共Int32 dwNumberofItems;
公共Int32 dwIndex;
公共WLAN_INTERFACE_INFO [] InterfaceInfo;

公用WLAN_INTERFACE_INFO_LIST(IntPtr pList)
{
//前4个字节是WLAN_INTERFACE_INFO结构的数量.
dwNumberofItems = Marshal.ReadInt32(pList,0);

//接下来的4个字节是非托管API中当前项目的索引.
dwIndex = Marshal.ReadInt32(pList,4);

//构造WLAN_INTERFACE_INFO结构的数组.
InterfaceInfo =新的WLAN_INTERFACE_INFO [dwNumberofItems];

for(int i = 0; i& lt; dwNumberofItems; i ++)
{
//结构数组的偏移量是从开头算起的8个字节.然后,获取索引并将其乘以结构中的字节数.
//WLAN_INTERFACE_INFO结构的长度为532字节-这是通过在非托管C ++应用中执行sizeof(WLAN_INTERFACE_INFO)来确定的.
IntPtr pItemList =新的IntPtr(pList.ToInt32()+(i * 532)+ 8);

//构造WLAN_INTERFACE_INFO结构,将非托管结构编组到其中,然后将其复制到结构数组中.
WLAN_INTERFACE_INFO wii =新的WLAN_INTERFACE_INFO();
wii =(WLAN_INTERFACE_INFO)Marshal.PtrToStructure(pItemList,typeof(WLAN_INTERFACE_INFO));
InterfaceInfo [i] = wii;
}
}
}

重建并运行代码,它将显示网卡的正确状态
Hi,

There was a minor bug in WLAN_INTERFACE_INFO structure layout definition. I have fixed this issue. You need to update the below two struct definitions for resolving ''not ready to operate'' status message:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WLAN_INTERFACE_INFO
{
/// GUID->_GUID
public Guid InterfaceGuid;

/// WCHAR[256]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strInterfaceDescription;

/// WLAN_INTERFACE_STATE->_WLAN_INTERFACE_STATE
public WLAN_INTERFACE_STATE isState;
}
/// <summary>
/// This structure contains an array of NIC information
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct WLAN_INTERFACE_INFO_LIST
{
public Int32 dwNumberofItems;
public Int32 dwIndex;
public WLAN_INTERFACE_INFO[] InterfaceInfo;

public WLAN_INTERFACE_INFO_LIST(IntPtr pList)
{
// The first 4 bytes are the number of WLAN_INTERFACE_INFO structures.
dwNumberofItems = Marshal.ReadInt32(pList, 0);

// The next 4 bytes are the index of the current item in the unmanaged API.
dwIndex = Marshal.ReadInt32(pList, 4);

// Construct the array of WLAN_INTERFACE_INFO structures.
InterfaceInfo = new WLAN_INTERFACE_INFO[dwNumberofItems];

for (int i = 0; i < dwNumberofItems; i++)
{
// The offset of the array of structures is 8 bytes past the beginning. Then, take the index and multiply it by the number of bytes in the structure.
// the length of the WLAN_INTERFACE_INFO structure is 532 bytes - this was determined by doing a sizeof(WLAN_INTERFACE_INFO) in an unmanaged C++ app.
IntPtr pItemList = new IntPtr(pList.ToInt32() + (i * 532) + 8);

// Construct the WLAN_INTERFACE_INFO structure, marshal the unmanaged structure into it, then copy it to the array of structures.
WLAN_INTERFACE_INFO wii = new WLAN_INTERFACE_INFO();
wii = (WLAN_INTERFACE_INFO)Marshal.PtrToStructure(pItemList, typeof(WLAN_INTERFACE_INFO));
InterfaceInfo[i] = wii;
}
}
}

Rebuild and run the code, it will show correct state of the network card


您可能会看到两个地方:

There are two places you might look:

  1)www. pinvoke.net-我为wlan api的一些功能发布了一些签名.我使用它们来查询我的接口,并确定它们是否已连接并获得信号强度.我使用了一个工具来生成pinvoke签名,然后对其进行了调整以使其正确.

 1) www.pinvoke.net - I posted some signatures for several of the functions for the wlan api. I use them to query my interfaces and determine whether they are connected and to get the signal strength.  I used a tool to generate the pinvoke signatures then tweaked them to get them correct.

 

2)您可能会在C#上的Codeplex上实现wlan api的实现,您可能会也用.但是,此代码是GPL,因此,如果您要进行商业开发,请提防此问题.

2) There is an implementation of the wlan api in C# on codeplex that you might also use. However, this code is GPL, so if you are doing commercial development beware of this.


这篇关于使用本机WiFi API访问无线网络参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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