我正在尝试从网络 GUID 获取可用的访问点,但我总是收到 DeviceIoControl() 的错误 87(错误参数)。这让我疯狂了一段时间,因为我不知道哪个参数是错误的!我已经搜索了几个小时,但找不到解决方案。代码如下:
PNDIS_802_11_BSSID_LIST getBssidList(wstring wsGuid, HANDLE & hNetAdapter, DWORD & dwMemSize) {
DWORD dwBytesReturned = 0;
DWORD oid = OID_802_11_BSSID_LIST;
PNDIS_802_11_BSSID_LIST pBssList;
wsGuid= L"\\\\.\\" + wsGuid;
hNetAdapter = CreateFileW(wsGuid.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, INVALID_HANDLE_VALUE) ;
if (hNetAdapter == INVALID_HANDLE_VALUE) {
return NULL;
}
// allocate temporary memory to check the number of AP entries
dwMemSize = sizeof(NDIS_802_11_BSSID_LIST) * 15;
pBssList = (NDIS_802_11_BSSID_LIST *) VirtualAlloc(NULL, dwMemSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
memset(pBssList, 0, dwMemSize);
// call get AP list
while (!DeviceIoControl(hNetAdapter, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, sizeof(oid), (ULONG *) pBssList, dwMemSize, &dwBytesReturned, NULL)) {
DWORD error = 0;
error = GetLastError();
if (error == ERROR_GEN_FAILURE || // Returned by some Intel cards.
error == ERROR_INSUFFICIENT_BUFFER ||
error == ERROR_MORE_DATA ||
error == NDIS_STATUS_INVALID_LENGTH ||
error == NDIS_STATUS_BUFFER_TOO_SHORT ) {
// free memory allocation and realloc
VirtualFree(pBssList, dwMemSize, MEM_RELEASE | MEM_DECOMMIT);
if (dwBytesReturned > dwMemSize) {
dwMemSize = dwBytesReturned;
} else {
dwMemSize *= 2;
}
pBssList = (NDIS_802_11_BSSID_LIST *) VirtualAlloc(NULL, dwMemSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
memset(pBssList, 0, dwMemSize);
} else {
// free memory allocation
VirtualFree(pBssList, dwMemSize, MEM_RELEASE | MEM_DECOMMIT);
CloseHandle(hNetAdapter);
pBssList = NULL;
break;
}
}
return pBssList;
}
我正在尝试使用 wsGuid = L"\\.\{8D36491D-C393-4D71-B10A-153C4FA69AEE}" 获取此列表,这是一个 Broadcom 802.11n 网络适配器。
编辑:我在 Win7 工作中尝试它。我知道它已被弃用(因此我还使用 WlanGetNetworkBssList() 为更高版本添加了可移植性,并且运行良好)。我在调试旧版本时遇到错误(在同一个 win7 工作站中),问题可能是:如果代码正确,NDIS IOCTL 是否仍在 Win7 及更高版本中工作?