之前写过一篇 Qt获取Windows系统的WIFI列表的文章,原理是通过cmd命令来实现的,现在发现直接调用Windows API会更简单,所以记录一下
Qt 获取WIFI列表:https://www.cnblogs.com/shiyixirui/p/17965357
代码:
#include <windows.h> #include <wlanapi.h> #include <objbase.h> #include <wtypes.h>#include <iostream> #include <vector>#pragma comment(lib, "wlanapi.lib") #pragma comment(lib, "ole32.lib")int test() {// 初始化COMHRESULT ret = CoInitializeEx(NULL, COINIT_MULTITHREADED);// Handle to the WLAN APIHANDLE hClient = NULL;DWORD dwMaxClient = 2, dwCurVersion = 0, dwResult = 0;// 打开WIFI模块dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);if (dwResult != ERROR_SUCCESS) { return; }// 枚举WIFI列表PWLAN_INTERFACE_INFO_LIST pIfList = NULL;dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);if (dwResult != ERROR_SUCCESS) { WlanCloseHandle(hClient, NULL); return; }// Iterate through the list of interfacesfor (DWORD i = 0; i < pIfList->dwNumberOfItems; i++) {WLAN_INTERFACE_INFO wlanIfInfo = pIfList->InterfaceInfo[i];// Get the list of available networksPWLAN_AVAILABLE_NETWORK_LIST pNetworkList = NULL;dwResult = WlanGetAvailableNetworkList(hClient, &wlanIfInfo.InterfaceGuid, 0, NULL, &pNetworkList);if (dwResult != ERROR_SUCCESS) { continue; }// Iterate through the list of available networksfor (DWORD j = 0; j < pNetworkList->dwNumberOfItems; j++) {WLAN_AVAILABLE_NETWORK network = pNetworkList->Network[j];QString name = QString::fromUtf8(QByteArray(reinterpret_cast<char*>(network.dot11Ssid.ucSSID)));qDebug() << "SSID:" << name;if (!name.isEmpty()) { ui.wifi_name->addItem(name); } }// 释放 pNetworkList WlanFreeMemory(pNetworkList);}WlanFreeMemory(pIfList); // 释放WIFI列表WlanCloseHandle(hClient, NULL); // 关闭WIFI模块CoUninitialize(); // 反初始化COM }