Hi all!
I'm using the Win32 Native Wi-Fi API and I've noticed a strange occurrence which has proven to be a bit of a pain and I can't say it's something on my end...
Simply put, when I retrieve a list of available networks using WlanGetAvailableNetworkList, I see a strange pattern unravelling in the _WLAN_AVAILABLE_NETWORK::strProfileName field of its members.
1) Regardless of the WLAN_AVAILABLE_NETWORK_HAS_PROFILE flag value, the strProfileName contains symbols and values, even though the docs state that "If the network does not have a profile, this member will be empty", which I assume means NULL-initialized.
2) I have 2 networks with a profile on my PC. The first such network's strProfileName is correct. The second is duplicated, i.e. it appears twice in strProfileName.
3) The strProfileName fields of networks with no profile contain the SSID of the previous such network in the returned list + other non-printable values, probably from the previous WLAN_AVAILABLE_NETWORK structure as well.
To illustrate, I have enclosed some screenshots where I have printed these fields in the console, as well as a screenshot of my project properties

. I have also enclosed the source code for anyone to try this out for themselves (but please replace my print_buf call with something of your own, as I cannot provide the source code for it).
I do not believe this happened in previous versions of the API, I have been using Native Wi-Fi for around 3 years at this point and I only just noticed the problem.
I get the feeling that the API decided to use this buffer for temporary storage and never cleaned it up. Whether this is a bug or not is up for debate, but it does cause problems and is not entirely... polite on the part of the API, tbh.
For example, my app allows the user to select a network which they would like to connect to upon start-up. I remember the network profile and store it in a file. The user may change this at any point later. Let's suppose they selected MINEPLEX3 first. Later, they decided to change that to Abay_Cicada. If Abay_Cicada appears in the list second and its profile name is muddled (as illustrated), my app will store the muddled profile name which does not exist in the system, meaning that I won't be able to connect to it.
Any additional info or advice on what to do would be greatly appreciated!!
Source code:
#include <Windows.h>
#include <wlanapi.h>
int main()
{
unsigned int work1;
PWLAN_AVAILABLE_NETWORK_LIST list = 0;
PWLAN_INTERFACE_INFO_LIST interfaces = 0;
HANDLE wifiHandle = 0; DWORD version = WLAN_API_VERSION;
work1 = WlanOpenHandle(WLAN_API_VERSION, NULL, &version, &wifiHandle);
if (work1 != 0) {
printf("\nFailed to start WiFi sub-system, error %x", work1);
return 0;
}
work1 = WlanEnumInterfaces(wifiHandle, NULL, &interfaces);
if (work1 != ERROR_SUCCESS) {
printf("\nCould not get available interfaces, error %x", work1); goto Mend;
}
work1 = WlanGetAvailableNetworkList(wifiHandle, &interfaces->InterfaceInfo[0].InterfaceGuid, NULL, NULL, &list);
if (work1 != 0) {
printf("\nCould not get network list, error %x", work1); goto Mend;
}
for (work1 = 0; work1 < list->dwNumberOfItems; work1++) {
if (GET_BITS(list->Network[work1].dwFlags, WLAN_AVAILABLE_NETWORK_HAS_PROFILE) != 0)
printf("\nHas profile!");
printf("\nSSID %s", &list->Network[work1].dot11Ssid.ucSSID);
print_buf(stdout, list->Network[work1].strProfileName, 0, sizeof(WLAN_AVAILABLE_NETWORK_LIST::Network->strProfileName));
}
Mend:
if (interfaces != 0) {
WlanFreeMemory(interfaces);
}
if (list != 0) {
WlanFreeMemory(list);
}
if (wifiHandle != 0) {
WlanCloseHandle(wifiHandle, NULL);
}
return 0;
}