I would like to know how a windows C++ app can get notified when windows system changes its wifi network. I'm interested in the following cases:
- When the user has switched on the wifi and has connected to a new network
- When the user has switched off the wifi and has disconnected from a network
- When the user has changed from network A to network B
Note: Switching on/off the wifi is not of interest. Device needs to be connected to a network. Network may or may not have an internet connection.
I'm trying to achieve this using the wlanapi.h and have checked out a few examples, but have not been able to achieve this.
Let me know if someone was able to achieve this using wlanapi.h. Or is there an another way? Pls demonstrate with an example.
Any help would be appreciated.
EDIT: Adding code
#include <windows.h>
#include <Wlanapi.h>
#include <iostream>
// Link wlanapi.lib
#pragma comment(lib, "wlanapi.lib")
void DetectWifiNetworkChanges ();
void EventLoop ();
// Callback func to receive network notifications
void WlanNotificationCallback (PWLAN_NOTIFICATION_DATA pData, PVOID pContext);
int main ()
{
DetectWifiNetworkChanges ();
printf ("\n");
getchar ();
return 0;
}
void DetectWifiNetworkChanges ()
{
printf ("DetectWifiNetworkChanges\n");
HANDLE client;
DWORD client_version = 2;
DWORD current_version = 0;
DWORD result;
result = WlanOpenHandle (client_version, NULL, ¤t_version, &client);
if (result != ERROR_SUCCESS) {
printf ("WlanOpenHandle failed !!\n");
return;
}
result = WlanRegisterNotification (client, WLAN_NOTIFICATION_SOURCE_ALL, FALSE, WlanNotificationCallback, NULL, NULL, NULL);
if (result != ERROR_SUCCESS) {
printf ("WlanRegisterNotification failed !!\n");
return;
}
printf ("WlanRegistration successful\n");
// An infinite loop (Ctrl + C to quit the app for now)
EventLoop ();
result = WlanCloseHandle (client, NULL);
if (result != ERROR_SUCCESS) {
printf ("WlanCloseHandle !!\n");
return;
}
}
Output
DetectWifiNetworkChanges
WlanRegistration successful
EventLoop
I tried to connect and disconnect from my network, but the callback is not called.
But sometimes, without me disconnecting from the network, the callback is called lots of times.
The solution was simple. I just had to change the type of notifications while registering and capture the states in the callback (The callback was empty previously).
When registering, the notification source should be WLAN_NOTIFICATION_SOURCE_ACM.
I captured all the states in the callback. Here is the modified working code
Observation:
When I click on the wifi icon to see the list of available networks, the callback is invoked with the following notifications
Note: WlanNotificationCallback, HandleACMNotifications are my printfs, as shown in the code. These two are pasted in the output along with the notification code as per the flow.
These notifications will be repeated since the system scans and displays the list of available networks in the following scenarios
Notifications received when disconnecting from the network:
Notifications received when connecting to a network:
Ignoring the intermediate notification codes, handling wlan_notification_acm_connection_complete and wlan_notification_acm_disconnected will work.