调用系统api接口实现
1、打开句柄WlanOpenHandle;
2、定义回调函数,用于处理连接消息
typedef VOID(WINAPI *CONNECT_NOTIFICATION_CALLBACK) (a,b);
3、注册消息通知回调函数 WlanRegisterNotification;
bool WifiTool::InitialHandle()
{
DWORD dwResult = 0;
DWORD dwCurVersion = 0;
DWORD dwMaxClient = 2;
if (m_wlanHandle == NULL)
{
if ((dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &m_wlanHandle)) != ERROR_SUCCESS)
{
std::cout << "wlanOpenHandle failed with error: " << dwResult << std::endl;
m_wlanHandle = NULL;
return false;
}
// 注册消息通知回调
if ((dwResult = WlanRegisterNotification(m_wlanHandle, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, WLAN_NOTIFICATION_CALLBACK(OnNotificationCallback), NULL, nullptr, nullptr)) != ERROR_SUCCESS)
{
std::cout << "wlanRegisterNotification failed with error: " << dwResult << std::endl;
m_wlanHandle = NULL;
return false;
}
}
return true;
}
4、消息通知回调处理 OnNotificationCallback;
void 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:
case wlan_notification_acm_scan_fail:
WifiTool::GetInstance().SetConnectResult(NULL, data->NotificationCode);
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:
{
PWLAN_CONNECTION_NOTIFICATION_DATA connection = (PWLAN_CONNECTION_NOTIFICATION_DATA)data->pData;
WifiTool::GetInstance().SetConnectResult(connection, data->NotificationCode);
}
break;
default:
break;
}
}
}
bool WifiTool::RigisterConnectNotification(CONNECT_NOTIFICATION_CALLBACK funcCallback)
{
if (funcCallback) {
m_funcConnectNotification = funcCallback;
}
return false;
}
bool WifiTool::SetConnectResult(PWLAN_CONNECTION_NOTIFICATION_DATA pData, DWORD dwConnectCode)
{
if (m_funcConnectNotification)
{
m_funcConnectNotification(pData, dwConnectCode);
}
return false;
}
5、接口及功能实现;
a、扫描无线网卡wifi热点 sacnwifi();
bool WifiTool::ScanWiFi()
{
std::unique_lock<std::mutex> lock(m_mutex);
if (!InitialHandle())//初始化Wlanhandle
{
std::cout << "initial wlan handle failed" << std::endl;
return false;
}
DWORD dwResult = 0;
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
WCHAR GuidString[39] = { 0 };
PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;
// 释放内存
CDefer pIfRaii([]() {}, [&pBssList, &pIfList]()
{
if (pBssList != NULL)
{
WlanFreeMemory(pBssList);
pBssList = NULL;
}
if (pIfList != NULL)
{
WlanFreeMemory(pIfList);
pIfList = NULL;
}
});
unsigned int i = 0;
//获取Wlan的网卡接口数据
dwResult = WlanEnumInterfaces(m_wlanHandle, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS)
{
return false;
}
for (i = 0; i < (int)pIfList->dwNumberOfItems; i++) //遍历每个网卡
{
pIfInfo = (WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i];
dwResult = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString,
sizeof(GuidString) / sizeof(*GuidString));
// 向无线网卡发送探测请求,WlanScan探测会在4秒内陆续更新WIFIList
dwResult = WlanScan(m_wlanHandle, (const GUID*)(&pIfInfo->InterfaceGuid), NULL, NULL, NULL);
if (dwResult != ERROR_SUCCESS)
{
return false;
}
}
return true;
}
b、获取热点信息getwifilist();
bool WifiTool::GetWiFiList(WiFiInfoList &wifilist)
{
wifilist.wifilist.clear();
std::unique_lock<std::mutex> lock(m_mutex);
if (!InitialHandle())//初始化Wlanhandle
{
std::cout << "initial wlan handle failed" << std::endl;
return false;
}
DWORD dwResult = 0;
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
WCHAR GuidString[39] = { 0 };
PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;
// 释放内存,pIfRaii对象析构时释放 pBssList, pIfList
CDefer pIfRaii([]() {}, [&pBssList, &pIfList]()
{