代码拉取完成,页面将自动刷新
/**
* Author: zhouwangsheng
* email: [email protected]
* date: 2023.05.25
*/
#include "wifitools.h"
#include <QDebug>
#pragma comment(lib, "wlanapi.lib")
#pragma comment(lib, "ole32.lib")
WifiTools * WifiTools::m_pInstance = NULL;
WifiTools::WifiTools()
{
qRegisterMetaType<BOOL>("BOOL");
qRegisterMetaType<GUID>("GUID");
m_pHandle = INVALID_HANDLE_VALUE;
m_WlanInterfaceList.clear();
m_ApList.clear();
}
WifiTools::~WifiTools()
{
if (m_pHandle != INVALID_HANDLE_VALUE)
{
WlanCloseHandle(m_pHandle, NULL);
}
}
WifiTools * WifiTools::instance()
{
if (NULL == m_pInstance)
{
m_pInstance = new WifiTools();
}
return m_pInstance;
}
DWORD WifiTools::wlanOpenHandle()
{
if (INVALID_HANDLE_VALUE != m_pHandle)
{
return 0;
}
DWORD dwMaxClient = 2;
DWORD dwCurVersion = 0;
// 参照:https://msdn.microsoft.com/en-us/library/windows/desktop/ms706759(v=vs.85).aspx
DWORD dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &m_pHandle);
return dwResult;
}
void WifiTools::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:
break;
case wlan_notification_acm_scan_fail:
emit WifiTools::instance()->WlanScanComplete(false, data->InterfaceGuid);
break;
case wlan_notification_acm_scan_list_refresh:
emit WifiTools::instance()->WlanScanComplete(true, data->InterfaceGuid);
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:
{
}
break;
default:
break;
}
qDebug() << "OnNotificationCallback:" << data->NotificationCode;
}
}
DWORD WifiTools::wlanRegisterNotification()
{
DWORD dwResult;
dwResult = WlanRegisterNotification(m_pHandle, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, WLAN_NOTIFICATION_CALLBACK(this->OnNotificationCallback), NULL, nullptr, nullptr);
if (dwResult != ERROR_SUCCESS)
{
qDebug() << "wlanRegisterNotification failed with error: " << dwResult;
return false;
}
return 0;
}
DWORD WifiTools::wlanEnumInterfaces()
{
if (INVALID_HANDLE_VALUE == m_pHandle)
{
qDebug("m_pHandle is INVALID_HANDLE_VALUE.");
return -1;
}
// 参照:https://msdn.microsoft.com/en-us/library/windows/desktop/ms706716(v=vs.85).aspx
PWLAN_INTERFACE_INFO_LIST ppIfList = NULL;
DWORD dwResult = WlanEnumInterfaces(m_pHandle, NULL, &ppIfList );
if (ERROR_SUCCESS == dwResult && ppIfList)
{
m_WlanInterfaceList.clear();
m_ApList.clear();
for (int i = 0; i < ppIfList->dwNumberOfItems; ++i)
{
WLAN_INTERFACE_DEV interfaceDev;
memcpy(&interfaceDev.InterfaceGuid, &(ppIfList->InterfaceInfo[i].InterfaceGuid), sizeof(GUID));
interfaceDev.isState = ppIfList->InterfaceInfo[i].isState;
interfaceDev.InterfaceDescriptionStr = QString::fromWCharArray(ppIfList->InterfaceInfo[i].strInterfaceDescription);
m_WlanInterfaceList.push_back(interfaceDev);
}
}
if (ppIfList != NULL) {
WlanFreeMemory(ppIfList);
ppIfList = NULL;
}
return dwResult;
}
DWORD WifiTools::wlanScan(int index)
{
if (INVALID_HANDLE_VALUE == m_pHandle)
{
qDebug("m_pHandle is INVALID_HANDLE_VALUE.");
return -1;
}
if(m_WlanInterfaceList.length() == 0)
{
qDebug("Wlan interface num is 0.");
return -1;
}
if(m_WlanInterfaceList.length() < (index+1))
{
qDebug("Wlan interface index bigger than num.");
return -1;
}
return WlanScan(m_pHandle,&m_WlanInterfaceList.at(index).InterfaceGuid,NULL,NULL,NULL);
}
DWORD WifiTools::wlanGetAvailableNetworkList(int index)
{
if (INVALID_HANDLE_VALUE == m_pHandle)
{
qDebug("m_pHandle is INVALID_HANDLE_VALUE.");
return -1;
}
PWLAN_AVAILABLE_NETWORK_LIST pNetList = NULL;
DWORD dwResult = WlanGetAvailableNetworkList(
m_pHandle,
&m_WlanInterfaceList.at(index).InterfaceGuid,
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES,
NULL,
&pNetList );
if(ERROR_SUCCESS != dwResult)
{
qDebug("WlanGetAvailableNetworkList failed. res=%d", dwResult);
return -1;
}
if(pNetList->dwNumberOfItems == 0)
{
qDebug("WlanGetAvailableNetworkList pNetList is zero.");
return -1;
}
if (pNetList && pNetList->dwNumberOfItems)
{
// 清空AP列表
m_ApList.clear();
for (int i = 0; i < pNetList->dwNumberOfItems; ++i)
{
WLAN_AP_INFO apInfo;
WLAN_AVAILABLE_NETWORK net = pNetList->Network[i];
PWLAN_BSS_LIST ppWlanBssList;
dwResult = WlanGetNetworkBssList(
m_pHandle,
&m_WlanInterfaceList.at(index).InterfaceGuid,
&net.dot11Ssid,
net.dot11BssType,
net.bSecurityEnabled,
NULL,
&ppWlanBssList);
if(ERROR_SUCCESS != dwResult) {
qDebug("WlanGetNetworkBssList failed.");
continue;
}
for (int z = 0; z < ppWlanBssList->dwNumberOfItems; z++)
{
WLAN_BSS_ENTRY bssEntry = ppWlanBssList->wlanBssEntries[z];
apInfo.pid = index;
apInfo.SSID = QString::fromUtf8(reinterpret_cast<char *>(net.dot11Ssid.ucSSID), net.dot11Ssid.uSSIDLength);
apInfo.dot11Ssid = net.dot11Ssid;
apInfo.bNetworkConnectable = net.bNetworkConnectable;
apInfo.strProfileName = QString::fromWCharArray(net.strProfileName);
apInfo.bSecurityEnabled = net.bSecurityEnabled;
apInfo.dot11BssType = bssEntry.dot11BssType;
apInfo.dot11DefaultAuthAlgorithm = net.dot11DefaultAuthAlgorithm;
apInfo.dot11DefaultCipherAlgorithm = net.dot11DefaultCipherAlgorithm;
apInfo.ulChCenterFrequency = bssEntry.ulChCenterFrequency;
apInfo.rssi = bssEntry.lRssi;
apInfo.BSSID = QString("%1:%2:%3:%4:%5:%6")
.arg(bssEntry.dot11Bssid[0], 2, 16, QLatin1Char('0'))
.arg(bssEntry.dot11Bssid[1], 2, 16, QLatin1Char('0'))
.arg(bssEntry.dot11Bssid[2], 2, 16, QLatin1Char('0'))
.arg(bssEntry.dot11Bssid[3], 2, 16, QLatin1Char('0'))
.arg(bssEntry.dot11Bssid[4], 2, 16, QLatin1Char('0'))
.arg(bssEntry.dot11Bssid[5], 2, 16, QLatin1Char('0'))
.toUpper();
m_ApList.push_back(apInfo);
}
}
}
return dwResult;
}
DWORD WifiTools::wlanGetProfile(WLAN_AP_INFO apInfo)
{
qDebug() << apInfo.pid;
LPWSTR pProfileXml = NULL;
DWORD dwFlags = 0;
DWORD dwGrantedAccess = 0;
DWORD dwResult = WlanGetProfile(
m_pHandle,
&m_WlanInterfaceList.at(apInfo.pid).InterfaceGuid,
apInfo.strProfileName.toStdWString().c_str(),
NULL,
&pProfileXml,
&dwFlags,
&dwGrantedAccess);
if (dwResult != ERROR_SUCCESS){
qDebug("WlanGetProfile failed. %d", dwResult);
}
qDebug() << QString::fromStdWString(pProfileXml);
return 0;
}
DWORD WifiTools::wlanSetProfile(WLAN_AP_INFO apInfo, QString pwd)
{
QString templateProfileXml("<?xml version=\"1.0\"?>\r\n<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\r\n\t<name>##SSID_NAME##</name>\r\n\t<SSIDConfig>\r\n\t\t<SSID>\r\n\t\t\t<name>##SSID_NAME##</name>\r\n\t\t</SSID>\r\n\t</SSIDConfig>\r\n\t<connectionType>ESS</connectionType>\r\n\t<connectionMode>manual</connectionMode>\r\n\t<MSM>\r\n\t\t<security>\r\n\t\t\t<authEncryption>\r\n\t\t\t\t<authentication>##AUTH_TYPE##</authentication>\r\n\t\t\t\t<encryption>##SECRITY_TYPE##</encryption>\r\n\t\t\t\t<useOneX>false</useOneX>\r\n\t\t\t</authEncryption>\r\n\t\t\t<sharedKey>\r\n\t\t\t\t<keyType>passPhrase</keyType>\r\n\t\t\t\t<protected>false</protected>\r\n\t\t\t\t<keyMaterial>##PASSWD##</keyMaterial>\r\n\t\t\t</sharedKey>\r\n\t\t</security>\r\n\t</MSM>\r\n</WLANProfile>");
templateProfileXml.replace("##SSID_NAME##", apInfo.SSID);
if(apInfo.dot11DefaultAuthAlgorithm == DOT11_AUTH_ALGO_80211_OPEN)
{
templateProfileXml.replace("##AUTH_TYPE##", "open");
templateProfileXml.replace("##SECRITY_TYPE##", "none");
} else {
templateProfileXml.replace("##AUTH_TYPE##", "WPA2PSK");
templateProfileXml.replace("##SECRITY_TYPE##", "AES");
}
templateProfileXml.replace("##PASSWD##", pwd);
DWORD dwReasonCode = 0;
DWORD dwResult = WlanSetProfile(
m_pHandle,
&m_WlanInterfaceList.at(apInfo.pid).InterfaceGuid,
0,
templateProfileXml.toStdWString().c_str(),
NULL,
false,
NULL,
&dwReasonCode);
if (dwResult != ERROR_SUCCESS){
qDebug("wlanSetProfile failed. %d, dwReasonCode=%x", dwResult, dwReasonCode);
qDebug() << templateProfileXml;
}
return dwResult;
}
DWORD WifiTools::wlanDeleteProfile(WLAN_AP_INFO apInfo)
{
if(apInfo.strProfileName.length() <= 0)
{
return 0;
}
DWORD dwResult = WlanDeleteProfile(
m_pHandle,
&m_WlanInterfaceList.at(apInfo.pid).InterfaceGuid,
apInfo.strProfileName.toStdWString().c_str(),
NULL);
return dwResult;
}
DWORD WifiTools::wlanConnect(WLAN_AP_INFO apInfo)
{
qDebug() << apInfo.pid;
WLAN_CONNECTION_PARAMETERS connParam;
// 连接模式
connParam.wlanConnectionMode = wlan_connection_mode_profile;
// 指定用于连接的配置文件
connParam.strProfile = (wchar_t*)reinterpret_cast<const wchar_t *>(apInfo.strProfileName.utf16());
// 指示网络的BSS类型.如果提供了配置文件,则此BSS类型必须与配置文件中的类型相同
connParam.dot11BssType = apInfo.dot11BssType;
connParam.pDot11Ssid = NULL;;
connParam.pDesiredBssidList = NULL;
// 用于指定连接参数的标志
connParam.dwFlags = WLAN_CONNECTION_HIDDEN_NETWORK;
DWORD dwResult = WlanConnect(m_pHandle, &m_WlanInterfaceList.at(apInfo.pid).InterfaceGuid, &connParam, NULL);
if (dwResult != ERROR_SUCCESS) {
qDebug("连接失败: %d", dwResult);
} else {
qDebug("连接成功");
}
return 0;
}
int WifiTools::TestFunc()
{
qDebug("Test Func begin. ");
DWORD dwResult = m_pInstance->wlanOpenHandle();
if(dwResult)
{
qDebug("wlanOpenHandle failed. res=%d", dwResult);
return dwResult;
}
dwResult = m_pInstance->wlanEnumInterfaces();
if(dwResult)
{
qDebug("wlanOpenHandle failed. res=%d", dwResult);
return dwResult;
}
dwResult = m_pInstance->wlanScan(0);
if(dwResult)
{
qDebug("wlanScan failed. res=%d", dwResult);
return dwResult;
}
dwResult = m_pInstance->wlanGetAvailableNetworkList();
// qDebug("wlan interface num=%d,%s",m_WlanInterfaceList.length(), m_WlanInterfaceList.at(0).InterfaceDescriptionStr.toLatin1().data());
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。