#include <Windows.h>
#include <wlanapi.h>
#pragma comment(lib, "Wlanapi.lib")
void findWIFiNameAndConnect()
{
// 初始化 WLAN API
DWORD dwMaxClient = 2; // 客户端版本为 Windows Vista 或更高版本
DWORD dwCurVersion = 0;
HANDLE hClient = NULL;
DWORD dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
return;
}
// 获取网络接口列表
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
WlanCloseHandle(hClient, NULL);
return ;
}
// 遍历网络接口列表
for (DWORD i = 0; i < pIfList->dwNumberOfItems; i++) {
PWLAN_INTERFACE_INFO pIfInfo = &pIfList->InterfaceInfo[i];
qDebug() << "Interface #" << i + 1;
qDebug() << "Interface Name: " << QString::fromWCharArray(pIfInfo->strInterfaceDescription);
// 获取接口的网络连接属性
PWLAN_AVAILABLE_NETWORK_LIST pAvailableNetworkList = NULL;
dwResult = WlanGetAvailableNetworkList(hClient, &pIfInfo->InterfaceGuid, 0, NULL, &pAvailableNetworkList);
if (dwResult == ERROR_SUCCESS) {
for (DWORD j = 0; j < pAvailableNetworkList->dwNumberOfItems; j++) {
PWLAN_AVAILABLE_NETWORK pNetwork = &pAvailableNetworkList->Network[j];
QString ssid = QString::fromUtf8(reinterpret_cast<const char*>(pNetwork->dot11Ssid.ucSSID));
QString profile = QString::fromUtf8(reinterpret_cast<const char*>(pNetwork->strProfileName));
qDebug() << "SSID: " << ssid << " profile:" << profile;
// 连接指定的wifi名称
if (ssid == "KO") {
#if 0
// 连接一个已经连过的wifi
PWLAN_CONNECTION_PARAMETERS pConnectionParams = new WLAN_CONNECTION_PARAMETERS();
pConnectionParams->wlanConnectionMode = wlan_connection_mode_profile ;
pConnectionParams->strProfile = pNetwork->strProfileName; // 替换为实际的 WiFi 配置文件名称
pConnectionParams->pDot11Ssid = &pNetwork->dot11Ssid; // 使用配置文件中的 SSID
pConnectionParams->pDesiredBssidList = NULL;
pConnectionParams->dot11BssType = dot11_BSS_type_infrastructure;
pConnectionParams->dwFlags = 0;
dwResult = WlanConnect(hClient, &pIfList->InterfaceInfo[i].InterfaceGuid, pConnectionParams, NULL);
if (dwResult != ERROR_SUCCESS) {
qDebug() << "WlanConnect failed with error code: " << dwResult;
} else {
qDebug() << "WlanConnect success";
break;
}
delete pConnectionParams;
#else
QString wifiName = "wifi名称";
QString wifiPassword = "wifi密码";
QString profile = QString("<?xml version=\"1.0\"?>"
"<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">"
"<name>%1</name>"
"<SSIDConfig>"
"<SSID>"
"<name>%2</name>"
"</SSID>"
"</SSIDConfig>"
"<connectionType>ESS</connectionType>"
"<connectionMode>auto</connectionMode>"
"<autoSwitch>false</autoSwitch>"
"<MSM>"
"<security>"
"<authEncryption>"
"<authentication>WPA2PSK</authentication>"
"<encryption>AES</encryption>"
"<useOneX>false</useOneX>"
"</authEncryption>"
"<sharedKey>"
"<keyType>passPhrase</keyType>"
"<protected>false</protected>"
"<keyMaterial>%3</keyMaterial>"
"</sharedKey>"
"</security>"
"</MSM>"
"</WLANProfile>").arg(wifiName).arg(wifiName).arg(wifiPassword);
// 设置Wlan配置文件
WLAN_REASON_CODE reasonCode;
std::wstring wstr = profile.toStdWString();
dwResult = WlanSetProfile(hClient, &pIfInfo->InterfaceGuid, 0, wstr.c_str(), NULL, TRUE, NULL, &reasonCode);
if (dwResult != ERROR_SUCCESS) {
qDebug() << "WlanSetProfile failed with error code: " << dwResult;
break;
}
// 连接一个未连过的wifi
PWLAN_CONNECTION_PARAMETERS pConnectionParams = new WLAN_CONNECTION_PARAMETERS();
pConnectionParams->wlanConnectionMode = wlan_connection_mode_profile ;
pConnectionParams->strProfile = wstr.c_str(); // 替换为实际的 WiFi 配置文件名称
DOT11_SSID dot11Ssid = { 0 };
// 将 QString 转换为宽字符串
std::wstring wifiNameW = wifiName.toStdWString();
// 拷贝宽字符串到 dot11Ssid
memcpy(dot11Ssid.ucSSID, wifiNameW.c_str(), wifiNameW.size() * sizeof(wchar_t));
dot11Ssid.uSSIDLength = static_cast<ULONG>(wifiNameW.size());
pConnectionParams->pDot11Ssid = &dot11Ssid; // 使用配置文件中的 SSID
pConnectionParams->pDesiredBssidList = NULL;
pConnectionParams->dot11BssType = dot11_BSS_type_infrastructure;
pConnectionParams->dwFlags = 0;
dwResult = WlanConnect(hClient, &pIfList->InterfaceInfo[i].InterfaceGuid, pConnectionParams, NULL);
if (dwResult != ERROR_SUCCESS) {
qDebug() << "WlanConnect failed with error code: " << dwResult;
break;
} else {
qDebug() << "WlanConnect success";
break;
}
delete pConnectionParams;
#endif
}
}
WlanFreeMemory(pAvailableNetworkList);
}
else {
qDebug() << "WlanGetAvailableNetworkList failed with error code: " << dwResult;
}
}
WlanFreeMemory(pIfList);
WlanCloseHandle(hClient, NULL);
}
qt搜索附近wifi名称并连接
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在上一小节中,笔者在介绍wifi通讯流程中介绍到,wifi通讯的本质就是由路由器(wifi芯片)担任局域网服务器来...
- 代码主要是造轮子的,没有什么需要讲解的,主要方便以后查询。 引入需要的头文件 检测WIFI功能是否打开 获取当前I...
- 项目要想获得WiFi信息先得打开一些权限1.添加依赖库target->buildphases->link Bina...
- 项目暂时告一段落,整理下大概功能及遇到的问题。 需求:给WiFi对讲机配网大概流程:1.获取本机连接的WiFi名称...
- 判断手机WiFi是否打开 获取WiFi名称 注意 需要引入的头文件 info.plist 文件需要配置获取的权限 ...