使用WLAN api中WlanGetAvailableNetworkList遇到的问题

Yokishiro 2014-04-27 10:21:56
api中的定义如下:

DWORD WINAPI WlanGetAvailableNetworkList(
_In_ HANDLE hClientHandle,
_In_ const GUID *pInterfaceGuid,
_In_ DWORD dwFlags,
_Reserved_ PVOID pReserved,
_Out_ PWLAN_AVAILABLE_NETWORK_LIST *ppAvailableNetworkList
);

typedef struct _WLAN_AVAILABLE_NETWORK_LIST {
DWORD dwNumberOfItems;
DWORD dwIndex;
WLAN_AVAILABLE_NETWORK Network[1];
} WLAN_AVAILABLE_NETWORK_LIST, *PWLAN_AVAILABLE_NETWORK_LIST;

typedef struct _WLAN_AVAILABLE_NETWORK {
WCHAR strProfileName[256];
DOT11_SSID dot11Ssid;
DOT11_BSS_TYPE dot11BssType;
ULONG uNumberOfBssids;
BOOL bNetworkConnectable;
WLAN_REASON_CODE wlanNotConnectableReason;
ULONG uNumberOfPhyTypes;
DOT11_PHY_TYPE dot11PhyTypes[WLAN_MAX_PHY_TYPE_NUMBER];
BOOL bMorePhyTypes;
WLAN_SIGNAL_QUALITY wlanSignalQuality;
BOOL bSecurityEnabled;
DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm;
DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm;
DWORD dwFlags;
DWORD dwReserved;
} WLAN_AVAILABLE_NETWORK, *PWLAN_AVAILABLE_NETWORK;

问题出在c#代码中我对struct _WLAN_AVAILABLE_NETWORK中,使用下面的定义方式,编译可以通过,在运行时会抛出异常:TypeLoadException 无法封送处理类型为“_WLAN_AVAILABLE_NETWORK”的字段“dot11PhyTypes1”: 无效的托管/非托管类型组合(Int32/UInt32 必须与 I4、U4 或 Error 成对出现)。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_AVAILABLE_NETWORK
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strProfileName;
public _DOT11_SSID dot11Ssid;
public _DOT11_BSS_TYPE dot11BssType;
public UInt64 uNumberOfBssids;
public int bNetworkConnectable;
public UInt32 wlanNotConnectableReason;
public UInt64 uNumberOfPhyTypes;
[MarshalAs(UnmanagedType.SysUInt, SizeConst = 8)]
public _DOT11_PHY_TYPE dot11PhyTypes1;
public int bMorePhyTypes;
public UInt64 wlanSignalQuality;
public int bSecurityEnabled;
public _DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm;
public _DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm;
public UInt32 dwFlags;
public UInt32 dwReserved;
}

[StructLayout(LayoutKind.Sequential)]
public struct WLAN_AVAILABLE_NETWORK_LIST
{
public Int32 dwNumberOfItems;
public Int32 dwIndex;
public _WLAN_AVAILABLE_NETWORK[] Network;

public WLAN_AVAILABLE_NETWORK_LIST(IntPtr pList)
{
// The first 4 bytes are the number of _WLAN_AVAILABLE_NETWORK 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_AVAILABLE_NETWORK structures.
Network = new _WLAN_AVAILABLE_NETWORK[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_AVAILABLE_NETWORK structure is 608 bytes - this
// was determined by doing a sizeof(_WLAN_AVAILABLE_NETWORK) in an
// unmanaged C++ app.
IntPtr pItemList = new IntPtr(pList.ToInt32() + (i * 608) + 8);

// Construct the WLAN_INTERFACE_INFO structure, marshal the unmanaged
// structure into it, then copy it to the array of structures.
_WLAN_AVAILABLE_NETWORK wii = new _WLAN_AVAILABLE_NETWORK();
wii = (_WLAN_AVAILABLE_NETWORK)Marshal.PtrToStructure(pItemList,
typeof(_WLAN_AVAILABLE_NETWORK)); //执行该语句的时候,抛出异常

Network[i] = wii;
}
}
}


如果将代码改为如下形式,则运行正常

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_AVAILABLE_NETWORK
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strProfileName;
public _DOT11_SSID dot11Ssid;
public _DOT11_BSS_TYPE dot11BssType;
public UInt64 uNumberOfBssids;
public int bNetworkConnectable;
public UInt32 wlanNotConnectableReason;
public UInt64 uNumberOfPhyTypes;
//[MarshalAs(UnmanagedType.SysUInt, SizeConst = 8)]
public _DOT11_PHY_TYPE dot11PhyTypes1;
public _DOT11_PHY_TYPE dot11PhyTypes2;
public _DOT11_PHY_TYPE dot11PhyTypes3;
public _DOT11_PHY_TYPE dot11PhyTypes4;
public _DOT11_PHY_TYPE dot11PhyTypes5;
public _DOT11_PHY_TYPE dot11PhyTypes6;
public _DOT11_PHY_TYPE dot11PhyTypes7;
public _DOT11_PHY_TYPE dot11PhyTypes8;
public int bMorePhyTypes;
public UInt64 wlanSignalQuality;
public int bSecurityEnabled;
public _DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm;
public _DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm;
public UInt32 dwFlags;
public UInt32 dwReserved;
}


这是什么原因?求解答
...全文
333 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
楼兰公子 2016-03-31
  • 打赏
  • 举报
回复
C#,我的是C++中的分配的内存释放冲突崩溃

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧