#pragma once
#include <windows.h>
#include <wlanapi.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "wlanapi.lib")
#define MODE_AUTO "auto"
#define MODE_MANUAL "manual"
#define AUTH_WPAPSK "WPAPSK"
#define AUTH_WPA2PSK "WPA2PSK"
#define ENCR_AES "AES"
#define ENCR_TKIP "TKIP"
struct WIFIInfo {
char account[64];
char password[64];
char mode[32];
char auth[32];
char encrypt[32];
};
class WifiMgr
{
public:
WifiMgr();
~WifiMgr();
bool connect(const char* account, const char* password, const char* mode = MODE_MANUAL,
const char* auth = AUTH_WPAPSK, const char* encrypt = ENCR_AES);
bool connect(const WIFIInfo& info);
bool reconnect();
bool disconnect(bool removeProFile = true);
bool connected();
const char* getLastError();
protected:
static void _stdcall wlanNotification(PWLAN_NOTIFICATION_DATA data, void* args);
private:
bool m_connected = false;
bool m_reconnect = false;
char m_lastError[256] = {
0 };
HANDLE m_handle = INVALID_HANDLE_VALUE;
PWLAN_INTERFACE_INFO_LIST m_infoList = {
0 };
WIFIInfo m_wifiInfo = {
0 };
};
#define _CRT_SECURE_NO_WARNINGS
#include "WifiMgr.h"
WifiMgr::WifiMgr()
{
}
WifiMgr::~WifiMgr()
{
if (m_connected)
{
disconnect();
}
}
bool WifiMgr::connect(const char* account, const char* password, const char* mode,
const char* auth, const char* encrypt)
{
bool result = false;
do
{
if (!m_reconnect)
{
strcpy(m_wifiInfo.account, account);
strcpy(m_wifiInfo.password, password);
strcpy(m_wifiInfo.mode, mode);
strcpy(m_wifiInfo.auth, auth);
strcpy(m_wifiInfo.encrypt, encrypt);
}
unsigned long version = 0, value = 0;
value = WlanOpenHandle(WLAN_API_VERSION, NULL, &version, &m_handle);
if (value != ERROR_SUCCESS)
{
sprintf(m_lastError, "打开WIFI句柄失败,错误代码:%lu", value);
break;