QT下WIFI管理工具

本文介绍了在QT中管理WiFi的步骤,包括调用WlanOpenHandle打开句柄,定义并注册CONNECT_NOTIFICATION_CALLBACK回调函数处理连接消息,以及实现扫描、获取热点信息、连接和断开WiFi热点的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用系统api接口实现

1、打开句柄WlanOpenHandle;

2、定义回调函数,用于处理连接消息

typedef VOID(WINAPI *CONNECT_NOTIFICATION_CALLBACK) (a,b);

3、注册消息通知回调函数 WlanRegisterNotification;

bool WifiTool::InitialHandle()
{
    DWORD dwResult = 0;
    DWORD dwCurVersion = 0;
    DWORD dwMaxClient = 2;
    if (m_wlanHandle == NULL)
    {
        if ((dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &m_wlanHandle)) != ERROR_SUCCESS)
        {
            std::cout << "wlanOpenHandle failed with error: " << dwResult << std::endl;
            m_wlanHandle = NULL;
            return false;
        }

        // 注册消息通知回调
        if ((dwResult = WlanRegisterNotification(m_wlanHandle, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, WLAN_NOTIFICATION_CALLBACK(OnNotificationCallback), NULL, nullptr, nullptr)) != ERROR_SUCCESS)
        {
            std::cout << "wlanRegisterNotification failed with error: " << dwResult << std::endl;
            m_wlanHandle = NULL;
            return false;
        }
    }
    return true;
}

4、消息通知回调处理 OnNotificationCallback;

void OnNotificationCallback(PWLAN_NOTIFICATION_DATA data, PVOID context)
{
    if (data != NULL && data->NotificationSource == WLAN_NOTIFICATION_SOURCE_ACM)
    {
        switch (data->NotificationCode)
        {
        case wlan_notification_acm_scan_complete:
        case wlan_notification_acm_scan_fail:
            WifiTool::GetInstance().SetConnectResult(NULL, data->NotificationCode);
            break;
        case wlan_notification_acm_connection_start:
        case wlan_notification_acm_connection_complete:
        case wlan_notification_acm_connection_attempt_fail:
        case wlan_notification_acm_disconnecting:
        case wlan_notification_acm_disconnected:
        {
            PWLAN_CONNECTION_NOTIFICATION_DATA connection = (PWLAN_CONNECTION_NOTIFICATION_DATA)data->pData;
            WifiTool::GetInstance().SetConnectResult(connection, data->NotificationCode);
        }
            break;
        default:
            break;
        }
    }
}
bool WifiTool::RigisterConnectNotification(CONNECT_NOTIFICATION_CALLBACK funcCallback)
{
    if (funcCallback) {
        m_funcConnectNotification = funcCallback;
    }
    return false;
}

bool WifiTool::SetConnectResult(PWLAN_CONNECTION_NOTIFICATION_DATA pData, DWORD dwConnectCode)
{
    if (m_funcConnectNotification)
    {
        m_funcConnectNotification(pData, dwConnectCode);
    }
    return false;
}

5、接口及功能实现;

        a、扫描无线网卡wifi热点 sacnwifi();

bool WifiTool::ScanWiFi()
{
    std::unique_lock<std::mutex> lock(m_mutex);
    if (!InitialHandle())//初始化Wlanhandle
    {
        std::cout << "initial wlan handle failed" << std::endl;
        return false;
    }

    DWORD dwResult = 0;
    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
    PWLAN_INTERFACE_INFO pIfInfo = NULL;
    WCHAR GuidString[39] = { 0 };

    PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
    PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;

    // 释放内存
    CDefer pIfRaii([]() {}, [&pBssList, &pIfList]()
    {
        if (pBssList != NULL)
        {
            WlanFreeMemory(pBssList);
            pBssList = NULL;
        }

        if (pIfList != NULL)
        {
            WlanFreeMemory(pIfList);
            pIfList = NULL;
        }
    });

    unsigned int i = 0;

    //获取Wlan的网卡接口数据
    dwResult = WlanEnumInterfaces(m_wlanHandle, NULL, &pIfList);
    if (dwResult != ERROR_SUCCESS)
    {
        return false;
    }

    for (i = 0; i < (int)pIfList->dwNumberOfItems; i++) //遍历每个网卡
    {
        pIfInfo = (WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i];

        dwResult = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString,
            sizeof(GuidString) / sizeof(*GuidString));

        // 向无线网卡发送探测请求,WlanScan探测会在4秒内陆续更新WIFIList
        dwResult = WlanScan(m_wlanHandle, (const GUID*)(&pIfInfo->InterfaceGuid), NULL, NULL, NULL);
        if (dwResult != ERROR_SUCCESS)
        {
            return false;
        }
    }

    return true;
}

        b、获取热点信息getwifilist();

bool WifiTool::GetWiFiList(WiFiInfoList &wifilist)
{
    wifilist.wifilist.clear();
    std::unique_lock<std::mutex> lock(m_mutex);
    if (!InitialHandle())//初始化Wlanhandle
    {
        std::cout << "initial wlan handle failed" << std::endl;
        return false;
    }

    DWORD dwResult = 0;
    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
    PWLAN_INTERFACE_INFO pIfInfo = NULL;
    WCHAR GuidString[39] = { 0 };

    PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
    PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;

    // 释放内存,pIfRaii对象析构时释放 pBssList, pIfList
    CDefer pIfRaii([]() {}, [&pBssList, &pIfList]()
    {
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值