首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows环境下的Wifi通知

Windows环境下的Wifi通知
EN

Stack Overflow用户
提问于 2021-10-09 13:29:42
回答 1查看 283关注 0票数 0

我想知道windows C++应用程序如何在windows系统更改其wifi网络时得到通知。我对下列案件感兴趣:

  1. 当用户打开wifi并连接到一个新的网络时
  2. 当用户关闭wifi并与网络断开连接时
  3. 当用户已从网络A变为网络B时

注意:打开/关闭wifi不感兴趣。设备需要连接到网络。网络可能有也可能没有互联网连接。

我正在尝试使用wlanapi.h来实现这个目标,并且已经检出了一些示例,但是没有能够做到这一点。

如果有人能用wlanapi.h实现这一点,请告诉我。还是有别的办法?请举例说明。

任何帮助都将不胜感激。

编辑:添加代码

代码语言:javascript
复制
#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, &current_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;
    }
}

输出

代码语言:javascript
复制
DetectWifiNetworkChanges
WlanRegistration successful
EventLoop

我试图连接和断开我的网络,但是没有调用回调。

但有时,如果没有我与网络的连接,回调就会被多次调用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-20 03:10:49

解决办法很简单。我只需在注册和捕获回调中的状态时更改通知类型(之前回调是空的)。

登记时,通知源应该是WLAN_NOTIFICATION_SOURCE_ACM。

我捕获了回调中的所有。以下是修改后的工作代码

代码语言:javascript
复制
#include    <windows.h>
#include    <Wlanapi.h>
#include    <iostream>

// Link wlanapi.lib

#pragma     comment(lib, "wlanapi.lib")

void    DetectWifiNetworkChanges ();
void    HandleACMNotifications (PWLAN_NOTIFICATION_DATA pNotification);

// Callback func to receive wifi notifications

void    WlanNotificationCallback (PWLAN_NOTIFICATION_DATA  pData, PVOID pContext);

int
main ()
{

    DetectWifiNetworkChanges ();

    printf ("\n");

    getchar ();
    return 0;
}

// Entry point.
// Will be called from int main
void
DetectWifiNetworkChanges ()
{
    printf ("DetectWifiNetworkChanges\n");

        HANDLE  client;
        DWORD   client_version  = 2;
        DWORD   current_version = 0;
        DWORD   result;

    result = WlanOpenHandle (client_version, NULL, &current_version, &client);
    if (result != ERROR_SUCCESS) {

        printf ("WlanOpenHandle failed !!\n");
        return;
    }

    result = WlanRegisterNotification (client, WLAN_NOTIFICATION_SOURCE_ACM, FALSE, WlanNotificationCallback, NULL, NULL, NULL);
    if (result != ERROR_SUCCESS) {

        printf ("WlanRegisterNotification failed !!\n");
        return;
    }

    printf ("WlanRegistration successful\n");

    // Wait for input from user
    // The program pauses here. Toggle the wifi, change wifi network etc.
    // to see the wifi notifications

    getchar ();

    result = WlanCloseHandle (client, NULL);
    if (result != ERROR_SUCCESS) {

        printf ("WlanCloseHandle !!\n");
        return;
    }
}

void
WlanNotificationCallback (PWLAN_NOTIFICATION_DATA pNotification, PVOID pContext)
{

    printf ("WlanNotificationCallback\n");

    if (pNotification->NotificationSource == WLAN_NOTIFICATION_SOURCE_ACM)
        HandleACMNotifications (pNotification);
}

void
HandleACMNotifications (PWLAN_NOTIFICATION_DATA pNotification)
{

    printf ("HandleACMNotifications\n");

    switch (pNotification->NotificationCode) {

        case wlan_notification_acm_start:
            printf ("wlan_notification_acm_start\n");
            break;

        case wlan_notification_acm_autoconf_enabled:
            printf ("wlan_notification_acm_autoconf_enabled\n");
            break;

        case wlan_notification_acm_autoconf_disabled:
            printf ("wlan_notification_acm_autoconf_disabled\n");
            break;

        case wlan_notification_acm_background_scan_enabled:
            printf ("wlan_notification_acm_background_scan_enabled\n");
            break;
                
        case wlan_notification_acm_background_scan_disabled:
            printf ("wlan_notification_acm_background_scan_disabled\n");
            break;

        case wlan_notification_acm_bss_type_change:
            printf ("wlan_notification_acm_bss_type_change\n");
            break;

        case wlan_notification_acm_power_setting_change:
            printf ("wlan_notification_acm_power_setting_change\n");
            break;

        case wlan_notification_acm_scan_complete:
            printf ("wlan_notification_acm_scan_complete\n");
            break;

        case wlan_notification_acm_scan_fail:
            printf ("wlan_notification_acm_scan_fail\n");
            break;

        case wlan_notification_acm_connection_start:
            printf ("wlan_notification_acm_connection_start\n");
            break;

        case wlan_notification_acm_connection_complete:
            printf ("wlan_notification_acm_connection_complete\n");
            break;

        case wlan_notification_acm_connection_attempt_fail:
            printf ("wlan_notification_acm_connection_attempt_fail\n");
            break;

        case wlan_notification_acm_filter_list_change:
            printf ("wlan_notification_acm_filter_list_change\n");
            break;

        case wlan_notification_acm_interface_arrival:
            printf ("wlan_notification_acm_interface_arrival\n");
            break;

        case wlan_notification_acm_interface_removal:
            printf ("wlan_notification_acm_interface_removal\n");
            break;

        case wlan_notification_acm_profile_change:
            printf ("wlan_notification_acm_profile_change\n");
            break;

        case wlan_notification_acm_profile_name_change:
            printf ("wlan_notification_acm_profile_name_change\n");
            break;

        case wlan_notification_acm_profiles_exhausted:
            printf ("wlan_notification_acm_profiles_exhausted\n");
            break;

        case wlan_notification_acm_network_not_available:
            printf ("wlan_notification_acm_network_not_available\n");
            break;

        case wlan_notification_acm_network_available:
            printf ("wlan_notification_acm_network_available\n");
            break;

        case wlan_notification_acm_disconnecting:
            printf ("wlan_notification_acm_disconnecting\n");
            break;

        case wlan_notification_acm_disconnected:
            printf ("wlan_notification_acm_disconnected\n");
            break;

        case wlan_notification_acm_adhoc_network_state_change:
            printf ("wlan_notification_acm_adhoc_network_state_change\n");
            break;

        case wlan_notification_acm_profile_unblocked:
            printf ("wlan_notification_acm_profile_unblocked\n");
            break;

        case wlan_notification_acm_screen_power_change:
            printf ("wlan_notification_acm_screen_power_change\n");
            break;

        case wlan_notification_acm_profile_blocked:
            printf ("wlan_notification_acm_profile_blocked\n");
            break;

        case wlan_notification_acm_scan_list_refresh:
            printf ("wlan_notification_acm_scan_list_refresh\n");
            break;

        case wlan_notification_acm_operational_state_change:
            printf ("wlan_notification_acm_operational_state_change\n");
            break;

        case wlan_notification_acm_end:
            printf ("wlan_notification_acm_end\n");
            break;
    }
}

观察:

当我单击wifi图标查看可用网络列表时,将使用以下通知调用回调

代码语言:javascript
复制
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_scan_complete
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_scan_list_refresh
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_network_available
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_profiles_exhausted
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_network_available

Note:WlanNotificationCallback,HandleACMNotifications是我的printfs,如代码所示。按照流程,这两种方法都与通知代码一起粘贴在输出中。

这些通知将被重复,因为系统扫描并显示下列方案中可用网络的列表

  1. 单击wifi图标选择并连接到网络时
  2. 当从网络断开连接时,可用网络列表再次可见。

断开网络时收到的通知:

代码语言:javascript
复制
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_scan_complete
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_scan_list_refresh
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_profile_change
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_disconnecting
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_disconnected

连接到网络时收到的通知:

代码语言:javascript
复制
wlan_notification_acm_profile_change
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_connection_start
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_scan_fail
WlanNotificationCallback
HandleACMNotifications
wlan_notification_acm_connection_complete

忽略中间通知代码,处理wlan_notification_acm_connection_complete和wlan_notification_acm_disconnected就可以了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69507147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档