Wlan连接
#include <wlanapi.h>
#pragma comment(lib, "wlanapi.lib")
BOOL connectWlan(CString Name, CString SSID, CString Password)
{
BOOL bRet = FALSE;
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
DWORD dwServiceVersion;
PWLAN_INTERFACE_INFO_LIST pIntfList = NULL;
// 打开WLAN服务
if ((dwError = WlanOpenHandle(WLAN_API_VERSION, NULL,&dwServiceVersion,&hClient)) == ERROR_SUCCESS) {
//枚举处当前系统安装的所有无线网卡的接口信息
if ((dwError = WlanEnumInterfaces(hClient,NULL,&pIntfList)) == ERROR_SUCCESS) {
for (int i = 0; i < pIntfList->dwNumberOfItems; i++)
{
/*获取网卡信息*/
PWLAN_INTERFACE_INFO pInterface = (PWLAN_INTERFACE_INFO)&pIntfList->InterfaceInfo[i];
/*获得网络信息列表*/
PWLAN_AVAILABLE_NETWORK_LIST pNetList = NULL;
DWORD dwResult = WlanGetAvailableNetworkList(hClient, &pInterface->InterfaceGuidGUID
,0x00, NULL, &pNetList);
if (ERROR_SUCCESS != dwResult)
{
if (pNetList != NULL)
{
WlanFreeMemory(pNetList);
pNetList = NULL;
}
break;
}
/*连接网络*/
for (int j = 0; j < pNetList->dwNumberOfItems; j++)
{
/*获取网络信息*/
PWLAN_AVAILABLE_NETWORK pNet = (PWLAN_AVAILABLE_NETWORK)&pNetList->Network[j];
if (strcmp(SSID, (char*)pNet->dot11Ssid.ucSSID))
continue;
if (pInterface->isState == wlan_interface_state_connected) {// 判断wifi是否所需
PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
DWORD dwSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;
dwResult = WlanQueryInterface(hClient,&pInterface->InterfaceGuid,wlan_intf_opcode_current_connection,NULL,&dwSize,(PVOID*)&pConnectInfo,&opCode);
if (ERROR_SUCCESS == dwResult)
{
if (pConnectInfo->wlanAssociationAttributes.dot11Ssid.uSSIDLength > 0) {
if (SSID.CompareNoCase((const char*)pConnectInfo->wlanAssociationAttributes.dot11Ssid.ucSSID) == 0) {
if (pIntfList != NULL)
{
WlanFreeMemory(pIntfList);
}
if (hClient != NULL)
{
WlanCloseHandle(hClient,NULL);
}
return true;
}
}
}
// 强制断开当前网络
WlanDisconnect(hClient, &pInterface->InterfaceGuid, NULL);
while (1) {
WlanFreeMemory(pIntfList);
if ((dwError = WlanEnumInterfaces(hClient,NULL,&pIntfList)) != ERROR_SUCCESS)
break;
pInterface = (PWLAN_INTERFACE_INFO)&pIntfList->InterfaceInfo[i];
if (pInterface->isState != wlan_interface_state_disconnected)
::Sleep(100);
else
break;
}
}
if (pInterface->isState == wlan_interface_state_disconnected)
{
// 连接wifi
USES_CONVERSION;
//Profile XML文件结构
CStringW szProfileXML(L"");
szProfileXML += CStringW(L"<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>");
/*name,一般与SSID相同*/
szProfileXML += CStringW(A2W(Name));
/*SSID*/
szProfileXML += CStringW(L"</name><SSIDConfig><SSID><name>");
szProfileXML += CStringW(A2W(SSID));
szProfileXML += CStringW(L"</name></SSID></SSIDConfig>");
/*网络类型*/
szProfileXML += CStringW(L"<connectionType>");
switch (pNet->dot11BssType)
{
case dot11_BSS_type_infrastructure:
szProfileXML += L"ESS";
break;
case dot11_BSS_type_independent:
szProfileXML += L"IBSS";
break;
case dot11_BSS_type_any:
szProfileXML += L"Any";
break;
default:
wprintf(L"Unknown BSS type");
return false;
}
//网络加密方式
szProfileXML += CStringW(L"</connectionType><MSM><security><authEncryption><authentication>");
switch (pNet->dot11DefaultAuthAlgorithm)
{
case DOT11_AUTH_ALGO_80211_OPEN:
szProfileXML += L"open";
break;
case DOT11_AUTH_ALGO_80211_SHARED_KEY:
szProfileXML += L"shared";
break;
case DOT11_AUTH_ALGO_WPA:
szProfileXML += L"WPA";
break;
case DOT11_AUTH_ALGO_WPA_PSK:
szProfileXML += L"WPAPSK";
break;
case DOT11_AUTH_ALGO_WPA_NONE:
szProfileXML += L"none";
break;
case DOT11_AUTH_ALGO_RSNA:
szProfileXML += L"WPA2";
break;
case DOT11_AUTH_ALGO_RSNA_PSK:
szProfileXML += L"WPA2PSK";
break;
default:
return false;
}
szProfileXML += CStringW(L"</authentication><encryption>");
switch (pNet->dot11DefaultCipherAlgorithm)
{
case DOT11_CIPHER_ALGO_NONE:
szProfileXML += L"none";
break;
case DOT11_CIPHER_ALGO_WEP40:
szProfileXML += L"WEP";
break;
case DOT11_CIPHER_ALGO_TKIP:
szProfileXML += L"TKIP";
break;
case DOT11_CIPHER_ALGO_CCMP:
szProfileXML += L"AES";
break;
case DOT11_CIPHER_ALGO_WEP104:
szProfileXML += L"WEP";
break;
case DOT11_CIPHER_ALGO_WEP:
szProfileXML += L"WEP";
break;
case DOT11_CIPHER_ALGO_WPA_USE_GROUP:
wprintf(L"USE-GROUP not exist in MSDN");
default:
if (pNetList != NULL)
{
WlanFreeMemory(pNetList);
pNetList = NULL;
}
return false;
}
szProfileXML += CStringW(L"</encryption></authEncryption>");
if (!Password.IsEmpty()) {
if (pNet->dot11DefaultCipherAlgorithm == DOT11_CIPHER_ALGO_WEP40
|| pNet->dot11DefaultCipherAlgorithm == DOT11_CIPHER_ALGO_WEP104
|| pNet->dot11DefaultCipherAlgorithm == DOT11_CIPHER_ALGO_WEP)
szProfileXML += CStringW(L"<sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>");
else
szProfileXML += CStringW(L"<sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>");
szProfileXML += CStringW(A2W(Password));
szProfileXML += CStringW(L"</keyMaterial></sharedKey>");
}
szProfileXML += CStringW(L"</security></MSM></WLANProfile>");
/*设置网络参数*/
DWORD dwReason;
dwError = WlanSetProfile(hClient,&pInterface->InterfaceGuid,0x00,szProfileXML, //用户文件内容
NULL,TRUE, //是否覆盖已存在的用户文件
NULL,&dwReason);
if (dwError != ERROR_SUCCESS) {
if (dwResult != ERROR_ALREADY_EXISTS)
{
return FALSE;
}
}
else {
/*设置网络连接参数*/
WLAN_CONNECTION_PARAMETERS wlanConnPara;
wlanConnPara.wlanConnectionMode = wlan_connection_mode_profile;
wlanConnPara.strProfile = A2W(SSID); // 指定的用户文件
wlanConnPara.pDot11Ssid = &pNet->dot11Ssid; //指定的SSID
wlanConnPara.dot11BssType = pNet->dot11BssType; //网络类型
wlanConnPara.pDesiredBssidList = NULL;
wlanConnPara.dwFlags = 0x00000000;
dwError = WlanConnect(hClient,&pInterface->InterfaceGuid,&wlanConnPara,NULL);
DWORD dwTimeout = GetTickCount64();
while (1) {
WlanFreeMemory(pIntfList);
if ((dwError = WlanEnumInterfaces(hClient,NULL,&pIntfList)) != ERROR_SUCCESS)
break;
pInterface = (PWLAN_INTERFACE_INFO)&pIntfList->InterfaceInfo[i];
if (pInterface->isState == wlan_interface_state_connected) {
bRet = TRUE;
break;
}
else {
if ((GetTickCount64() - dwTimeout) >= 30000)
break;
::Sleep(100);
}
}
}
break;
}
}
}
}
}
if (pIntfList != NULL)
{
WlanFreeMemory(pIntfList);
}
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL
);
}
return bRet;
}本篇文章链接 地址:https://wmzos.com/?id=41

如果有帮助到您,打赏一下作者吧~
添加新评论