Qt获取wifi列表,连接wifi后获取IP地址

环境win7+qt5.13+MinGW32,台式机

直接上步骤了,网上看到的略显啰嗦,就是这么简单直接:

1、头文件 #include "windows.h",#include "wlanapi.h"

2、.pro设置连接路径(需要根据自己安装路径加载

LIBS += C:/Qt/Qt5.13.0/Tools/mingw730_32/i686-w64-mingw32\lib\libwlanapi.a

3、显示在combox控件内

ui->cbx_WifiName->clear();
    HANDLE  hClientHandle;
    DWORD dwError = ERROR_SUCCESS;//原来我DWORD MFC的数据类型,相当于unsigned long
    DWORD dwNegotiatedVersion;

    dwError = WlanOpenHandle(1, nullptr, &dwNegotiatedVersion, &hClientHandle);

    //连接不成功
    if (dwError != ERROR_SUCCESS)
    {
        qDebug()<<"未打开成功!";
        WlanCloseHandle(hClientHandle,nullptr);
        return;
    }
    PWLAN_INTERFACE_INFO_LIST  pInterfaceList;
    dwError = WlanEnumInterfaces(hClientHandle, nullptr,&pInterfaceList);
    //返回值还是DWRD类型的,成功的话是ERROR_SUCCESS
    if ( dwError != ERROR_SUCCESS )
    {
        ErroInf="未发现wifi相关列表!";
        WlanFreeMemory(pInterfaceList);//释放列表
        //释放内存,从Native Wifi函数返回的任何内存必须释放
        WlanCloseHandle(hClientHandle,nullptr);//关闭wlan
        return;
    }
    GUID &guid = pInterfaceList->InterfaceInfo[0].InterfaceGuid;
    PWLAN_AVAILABLE_NETWORK_LIST pWLAN_AVAILABLE_NETWORK_LIST = nullptr;

    WlanGetAvailableNetworkList(hClientHandle, &guid,2,nullptr, &pWLAN_AVAILABLE_NETWORK_LIST);

    //添加到combox
    for (int i=0;idwNumberOfItems;i++)
    {
        ui->cbx_WifiName->addItem((char*)(pWLAN_AVAILABLE_NETWORK_LIST->Network[i].dot11Ssid.ucSSID));
    }

4、上图

Qt获取wifi列表,连接wifi后获取IP地址_第1张图片

读取IP地址

ui->cbx_Ip->clear();
    QString localHostName = QHostInfo::localHostName();
    qDebug() <<"localHostName:"<cbx_Ip->addItem(address.toString());
    }

链接:Qt获取WIFI名称列表的实现方法-C++文档类资源-CSDN下载 

你可能感兴趣的:(Qt小功能,qt,wifi)