admin 管理员组

文章数量: 887020

环境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);
    
    if(pWLAN_AVAILABLE_NETWORK_LIST==nullptr)   //防止程序崩溃
        return;
    //添加到combox
    for (int i=0;i<pWLAN_AVAILABLE_NETWORK_LIST->dwNumberOfItems;i++)
    {
        ui->cbx_WifiName->addItem((char*)(pWLAN_AVAILABLE_NETWORK_LIST->Network[i].dot11Ssid.ucSSID));
    }

4、上图

读取IP地址

ui->cbx_Ip->clear();
    QString localHostName = QHostInfo::localHostName();
    qDebug() <<"localHostName:"<<localHostName;
    QHostInfo info = QHostInfo::fromName(localHostName);
    foreach(QHostAddress address,info.addresses())
    {
        if(address.protocol() == QAbstractSocket::IPv4Protocol)
            ui->cbx_Ip->addItem(address.toString());
    }

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

本文标签: 地址 列表 QT WiFi IP