赞
踩
本单元只介绍如何链接wifi设备,对信号强度不做涉及,另外链接指定wifi之前应该先判断wifi是否已经链接,如果已经链接就没必要再链接了,代码中采用的是没做链接判断,先直接断开然后重新进行链接,代码如下:
- #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_O
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。