我試圖從網絡GUID獲得可用的接入點,但我總是得到DeviceIoControl()錯誤87(錯誤的參數)。這讓我發瘋了一段時間,因爲我不知道哪個參數錯了!我一直在谷歌搜索幾個小時,並找不到解決方案。代碼如下: 「\\。\ {8D36491D-C393-4D71-B10A-153C4FA69AEE}」DeviceIoControl錯誤87爲OID_802_11_BSSID_LIST
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這是博通802.11n網絡適配器。
編輯:我試着在Win7工作。我知道它已被棄用(所以我也爲WlanGetNetworkBssList()的更新版本添加了可移植性,並且它運行良好)。在調試老版本時(在這個相同的win7工作站中),我得到了錯誤,也許問題是:如果代碼是正確的,NDIS IOCTL它仍然在Win7和更高版本中工作?
您必須改用NDIS_802_11_BSSID_LIST_EX。 15是任意的,太低,實際結構要大得多。 –
@HansPassant謝謝,也試過用NDIS_802_11_BSSID_LIST_EX和相同的結果:錯誤87.關於15,如果我沒有錯,我應該得到ERROR_INSUFFICIENT_BUFFER或ERROR_MORE_DATA,直到我達到正確的緩衝區大小,不是嗎?無論如何,我已經嘗試過20,100,1000,並且在DeviceIoControl的第一次調用時我總是得到錯誤87。 – Miquel