我已经映射了 C++ 函数(来自 WLanapi.dll):
DWORD WINAPI WlanHostedNetworkQueryStatus(
_In_ HANDLE hClientHandle,
_Out_ PWLAN_HOSTED_NETWORK_STATUS *ppWlanHostedNetworkStatus,
_Reserved_ PVOID pvReserved
);
到以下 C# 代码:
[DllImport("Wlanapi.dll", SetLastError = true)]
static extern UInt32 WlanHostedNetworkQueryStatus(
[In] IntPtr hClientHandle,
[Out] out _WLAN_HOSTED_NETWORK_STATUS ppWlanHostedNetworkStatus,
[In, Out] IntPtr pvReserved
);
我还映射了所需的所有结构和枚举以及其他内容(例如,获取 clientHandle 指针并启动托管网络)。
_WLAN_HOSTED_NETWORK_STATUS 映射如下:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_HOSTED_NETWORK_STATUS
{
public _WLAN_HOSTED_NETWORK_STATE HostedNetworkState;
public Guid IPDeviceID;
public _DOT11_MAC_ADDRESS wlanHostedNetworkBSSID;
public _DOT11_PHY_TYPE dot11PhyType;
public UInt32 ulChannelFrequency;
public UInt32 dwNumberOfPeers;
public _WLAN_HOSTED_NETWORK_PEER_STATE[] PeerList;
}
现在执行该功能时,我不确定如何正确使用 ppWlanHostedNetworkStatus 等。该函数返回 ERROR_SUCCESS (0) 这意味着我已经调用它并正确传递了参数:
_WLAN_HOSTED_NETWORK_STATUS netStatus = new _WLAN_HOSTED_NETWORK_STATUS();
WlanHostedNetworkQueryStatus(clientHandle, out netStatus, IntPtr.Zero);
但是在查询 ppWlanHostedNetworkStatus 的值(如网络状态或已连接对等点的数量)时,我得到了一些奇怪的长整数(我会说内存地址,但我不确定),例如调用:
netStatus.HostedNetworkState.ToString();
退货
11465720
HostedNetworkState 是一个枚举,定义如下:
public enum _WLAN_HOSTED_NETWORK_STATE
{
wlan_hosted_network_unavailable,
wlan_hosted_network_idle,
wlan_hosted_network_active
}
所以 .toString() 应该从枚举中返回这些字符串之一,对吧?
我很确定这与指针等有关,因为在 _WLAN_HOSTED_NETWORK_STATUS 的文档(MS 文档)中说,在调用该函数之前,ppWlanHostedNetworkStatus 应该为 NULL,并且它本身就是指向结构体...
我该如何调试它?我正在用 C#、VS 2012 编写代码...
谢谢你的帮助。
-----编辑-----
我进一步尝试使用 IntPtr 作为参数来映射函数,传递 IntPtr.Zero 和 Marshal.PtrToStruct,但是在尝试这样做时我得到了 AccessViolationException...
[DllImport("Wlanapi.dll", SetLastError = true)]
static extern UInt32 WlanHostedNetworkQueryStatus(
[In] IntPtr hClientHandle,
[Out] out IntPtr ppWlanHostedNetworkStatus,
[In, Out] IntPtr pvReserved
);
进而:
IntPtr ppStatus = IntPtr.Zero;
WlanHostedNetworkQueryStatus(clientHandle, out ppStatus, IntPtr.Zero);
_WLAN_HOSTED_NETWORK_STATUS netStatus = (_WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ppStatus, typeof(_WLAN_HOSTED_NETWORK_STATUS));
------编辑2------
根据 Fermat2357 的建议,我取消了要映射的结构的部分注释,并更改以下内容以计算指向指针的指针:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_HOSTED_NETWORK_STATUS
{
public _WLAN_HOSTED_NETWORK_STATE HostedNetworkState;
public Guid IPDeviceID;
public _DOT11_MAC_ADDRESS wlanHostedNetworkBSSID;
public _DOT11_PHY_TYPE dot11PhyType;
public UInt32 ulChannelFrequency;
public UInt32 dwNumberOfPeers;
//public _WLAN_HOSTED_NETWORK_PEER_STATE[] PeerList;
}
我这样称呼它:
IntPtr ppStatus = IntPtr.Zero;
WlanHostedNetworkQueryStatus(clientHandle, out ppStatus, IntPtr.Zero);
IntPtr ppStatus2 = new IntPtr(ppStatus.ToInt32());
_WLAN_HOSTED_NETWORK_STATUS stat = (_WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ppStatus2, typeof(_WLAN_HOSTED_NETWORK_STATUS));
netStatus = stat.HostedNetworkState.ToString();
这终于给了我正确的网络状态(启动后处于活动状态)......现在我必须找到一种方法来编组该动态数组......
到目前为止感谢Fermat2357的帮助