VC++管理Wifi方法讲解
程序员文章站
2022-04-08 10:26:04
VC++管理Wifi
使用wlan native api来连接、断开、枚举Wifi信息。
这里只处理连接过的Wifi,没有链接过的不考虑。
PS: CSDN博客频道真是越改越...
VC++管理Wifi
使用wlan native api来连接、断开、枚举Wifi信息。
这里只处理连接过的Wifi,没有链接过的不考虑。
PS: CSDN博客频道真是越改越烂!Fuck!
代码
// Demo1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include #include #include #pragma comment(lib, "ole32.lib") #pragma comment(lib, "wlanapi.lib") void ssid2wchar(DOT11_SSID& pIn, WCHAR* pOut) { char tmp[33] = { 0 }; //for (int i=0;i< pIn.uSSIDLength; i++) //{ // sprintf(tmp+i, "%02X", pIn.ucSSID[i]&0xFF); //} memcpy(tmp, pIn.ucSSID, pIn.uSSIDLength); MultiByteToWideChar(CP_ACP, 0, tmp, strlen(tmp), pOut, 65); } int main() { CoInitialize(NULL); DWORD dwResult = 0; DWORD dwCurVersion = 0; HANDLE hClient = NULL; WCHAR GuidString[48] = { 0 }; PWLAN_INTERFACE_INFO_LIST pIfList = NULL; dwResult = WlanOpenHandle(2, NULL, &dwCurVersion, &hClient); if (dwResult != ERROR_SUCCESS) { wprintf(L"open wlan error!: %u\n", dwResult); goto end; } WlanEnumInterfaces(hClient, NULL, &pIfList); // 枚举WLAN设备 StringFromGUID2((pIfList->InterfaceInfo[0].InterfaceGuid), (LPOLESTR)&GuidString, sizeof(GuidString) / sizeof(*GuidString)); wprintf(L"Wlan nums: %u name[0]: %s GUID: %s\n", pIfList->dwNumberOfItems, pIfList->InterfaceInfo[0].strInterfaceDescription, GuidString); // 连接or断开wifi if (wlan_interface_state_connected == pIfList->InterfaceInfo[0].isState) { //WlanDisconnect(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid, NULL);//DISCONNECT FIRST }else if (wlan_interface_state_disconnected == pIfList->InterfaceInfo[0].isState) { WLAN_CONNECTION_PARAMETERS wlanConnPara; wlanConnPara.wlanConnectionMode = wlan_connection_mode_profile; //YES,WE CONNECT AP VIA THE PROFILE wlanConnPara.strProfile = L"Tami1703-5G"; // set the profile name wlanConnPara.pDot11Ssid = NULL; // SET SSID NULL wlanConnPara.dot11BssType = dot11_BSS_type_infrastructure; //dot11_BSS_type_any,I do not need it this time. wlanConnPara.pDesiredBssidList = NULL; // the desired BSSID list is empty wlanConnPara.dwFlags = WLAN_CONNECTION_HIDDEN_NETWORK; //it works on my WIN7\8 WlanConnect(hClient, &(pIfList->InterfaceInfo[0].InterfaceGuid), &wlanConnPara, NULL); Sleep(2000); } // scan wifi skip, use hand // list wifi PWLAN_AVAILABLE_NETWORK_LIST pWifiList = NULL; dwResult = WlanGetAvailableNetworkList(hClient, &(pIfList->InterfaceInfo[0].InterfaceGuid), WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES, NULL, &pWifiList); if (dwResult == ERROR_SUCCESS) { wprintf(L"Wifi Nums: %u\n", pWifiList->dwNumberOfItems); for (int i=0; i < pWifiList->dwNumberOfItems;i++) { WLAN_AVAILABLE_NETWORK oneWifi = pWifiList->Network[i]; WCHAR pBuff[65] = { 0 }; ssid2wchar(oneWifi.dot11Ssid, pBuff); wprintf(L"[%d] Name: %s,\t Qos: %u, SSID: %s\n", i, lstrlen(oneWifi.strProfileName)>0?oneWifi.strProfileName:L"null", oneWifi.wlanSignalQuality, pBuff); } } end: system("\npause"); CoUninitialize(); return 0; }