I'm trying to get the available access points from a network GUID but I'm always getting error 87 (wrong param) for DeviceIoControl(). It's getting me crazy for a while, as I don't know which param is wrong! I've been googling for hours and can't find the solution. The code is the following:
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;
}
I'm trying to get this list with wsGuid = L"\\.\{8D36491D-C393-4D71-B10A-153C4FA69AEE}" which is a Broadcom 802.11n Network Adapter.
EDIT: I'm trying it in a Win7 worksation. I know it is deprecated (and so I also added portability for later versions with WlanGetNetworkBssList() and it's working well). I'm getting the error when debugging for older versions (in this same win7 workstation), maybe the question is: If the code is correct, is NDIS IOCTL it still working in Win7 and later?