C++操作Windows WIFI

原文链接地址:https://blog.csdn.net/just_do_1122/article/details/78031024

实现功能
    无线网卡列表
    无线热点扫面
    无线连接(有密码,配置文件连接方式)
    无线断开
    重命名本地无线名(两种方式)
    删除无线配置文件
    开启和关闭无线网卡
Native Wifi 简介
    是提供给软件开发者来开发windows 无线管理的一系列API。编程人员可以通过这些函数来进行相关的无线管理,当然我们还可以通过netsh终端命令来管理,这对于非编程人员就可以简单的实现,具体可以查阅相关资料去了解。
    API如下:
无线的连接相关知识
 

 

 

从windows的无线网络属性设置窗口来对比,在API编程中,同样有个配置文件来设置这些属性的,那就是profile文件,通过编写xml文件来设置相关属性。

WLAN_profile Schema Elements [xml配置文件编写格式]:

    

Wireless Profile Samples[无线配置文件例程]

 1 <?xml version="1.0" encoding="US-ASCII"?>
 2 <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
 3     <name>SampleWPA2PSK</name>
 4     <SSIDConfig>
 5         <SSID>
 6             <name>SampleWPA2PSK</name>
 7         </SSID>
 8     </SSIDConfig>
 9     <connectionType>ESS</connectionType>
10     <connectionMode>auto</connectionMode>
11     <autoSwitch>false</autoSwitch>
12     <MSM>
13         <security>
14             <authEncryption>
15                 <authentication>WPA2PSK</authentication>
16                 <encryption>AES</encryption>
17                 <useOneX>false</useOneX>
18             </authEncryption>
19  <sharedKey>
20      <keyType>passPhrase</keyType>
21      <protected>false</protected>
22      <keyMaterial> <!-- insert key here --> </keyMaterial>
23  </sharedKey>
24          </security>
25     </MSM>
26 </WLANProfile>
注:
【加密类型设置】
<encryption>AES</encryption>
    【安全类型设置】
<authentication>WPAPSK</authentication>
ValueDescription
open Open 802.11 authentication.
shared Shared 802.11 authentication.
WPA WPA-Enterprise 802.11 authentication.
WPAPSK WPA-Personal 802.11 authentication.
WPA2 WPA2-Enterprise 802.11 authentication.
WPA2PSK WPA2-Personal 802.11 authentication.
具体功能编程实现(QTCtreator 5.xx)
    .pro文件
    LIBS+=$$quote(E:/qt/2015-4-9/WlanGetProfileTest/WlanGetProfileTest/lib/wlanapi.lib)
    LIBS+=$$quote(E:/qt/2015-4-9/WlanGetProfileTest/WlanGetProfileTest/lib/OLE32.lib)
    包含头文件
    #include<windows.h>
    #include<wlanapi.h>
  1 程序实现(终端打印信息)
  2 #include<windows.h>
  3 #include<wlanapi.h>
  4 #include<string>
  5 #include<stdio.h>
  6 
  7 //无线连接状态
  8 intlistenStatus()
  9 {
 10     HANDLEhClient=NULL;
 11     DWORDdwMaxClient=2;
 12     DWORDdwCurVersion=0;
 13     DWORDdwResult=0;
 14     intiRet=0;
 15     WCHARGuidString[39]={0};
 16     //ListenthestatusoftheAPyouconnected.
 17     while(1){
 18         Sleep(5000);
 19         PWLAN_INTERFACE_INFO_LISTpIfList=NULL;
 20         PWLAN_INTERFACE_INFOpIfInfo=NULL;
 21 
 22         dwResult=WlanOpenHandle(dwMaxClient,NULL,&dwCurVersion,&hClient);
 23         if(dwResult!=ERROR_SUCCESS){
 24             wprintf(L"WlanOpenHandlefailedwitherror:%u\n",dwResult);
 25             return1;
 26         }
 27         //获取无线网卡列表
 28         dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);
 29         if(dwResult!=ERROR_SUCCESS){
 30             wprintf(L"WlanEnumInterfacesfailedwitherror:%u\n",dwResult);
 31             return1;
 32         }else{
 33             wprintf(L"NumEntries:%lu\n",pIfList->dwNumberOfItems);
 34             wprintf(L"CurrentIndex:%lu\n\n",pIfList->dwIndex);
 35             inti;
 36             for(i=0;i<(int)pIfList->dwNumberOfItems;i++){
 37                 pIfInfo=(WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i];
 38                 wprintf(L"InterfaceIndex[%u]:\t%lu\n",i,i);
 39                 iRet=StringFromGUID2(pIfInfo->InterfaceGuid,(LPOLESTR)&GuidString,
 40                     sizeof(GuidString)/sizeof(*GuidString));
 41                 if(iRet==0)
 42                     wprintf(L"StringFromGUID2failed\n");
 43                 else{
 44                     wprintf(L"InterfaceGUID[%d]:%S\n",i,GuidString);
 45                 }
 46                 wprintf(L"InterfaceDescription[%d]:%S",i,
 47                     pIfInfo->strInterfaceDescription);
 48                 wprintf(L"\n");
 49                 wprintf(L"InterfaceState[%d]:\t",i);
 50                 switch(pIfInfo->isState){
 51 casewlan_interface_state_not_ready:
 52                     wprintf(L"Notready\n");
 53                     break;
 54 casewlan_interface_state_connected:
 55                     wprintf(L"Connected\n");
 56                     break;
 57 casewlan_interface_state_ad_hoc_network_formed:
 58                     wprintf(L"Firstnodeinaadhocnetwork\n");
 59                     break;
 60 casewlan_interface_state_disconnecting:
 61                     wprintf(L"Disconnecting\n");
 62                     break;
 63 casewlan_interface_state_disconnected:
 64                     wprintf(L"Notconnected\n");
 65                     break;
 66 casewlan_interface_state_associating:
 67                     wprintf(L"Attemptingtoassociatewithanetwork\n");
 68                     break;
 69 casewlan_interface_state_discovering:
 70                     wprintf(L"Autoconfigurationisdiscoveringsettingsforthenetwork\n");
 71                     break;
 72 casewlan_interface_state_authenticating:
 73                     wprintf(L"Inprocessofauthenticating\n");
 74                     break;
 75 default:
 76     wprintf(L"Unknownstate%ld\n",pIfInfo->isState);
 77     break;
 78                 }
 79             }
 80         }
 81     }
 82 }
 83 intmain()
 84 {
 85     HANDLEhClient=NULL;
 86     DWORDdwMaxClient=2;
 87     DWORDdwCurVersion=0;
 88     DWORDdwResult=0;
 89     PWLAN_INTERFACE_INFO_LISTpIfList=NULL;
 90     //opensaconnectiontotheserver.
 91     dwResult=WlanOpenHandle(dwMaxClient,NULL,&dwCurVersion,&hClient);
 92     if(dwResult!=ERROR_SUCCESS){
 93         wprintf(L"WlanOpenHandlefailedwitherror:%u\n",dwResult);
 94         return1;
 95     }
 96     //enumeratesallofthewirelessLANinterfacescurrentlyenabledonthelocalcomputer.
 97     dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);
 98     if(dwResult!=ERROR_SUCCESS){
 99         wprintf(L"WlanEnumInterfacesfailedwitherror:%u\n",dwResult);
100         return1;
101     }else{
102         //disconnectsaninterfacefromitscurrentnetwork.
103         dwResult=WlanDisconnect(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,NULL);//DISCONNECTFIRST
104         if(dwResult!=ERROR_SUCCESS)
105         {
106             printf("WlanDisconnectfailedwitherror:%lu\n",dwResult);
107             return-1;
108         }
109         //retrievethelistofavailablenetworksonawirelessLANinterface.
110         PWLAN_AVAILABLE_NETWORK_LISTpWLAN_AVAILABLE_NETWORK_LIST=NULL;
111         dwResult=WlanGetAvailableNetworkList(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,
112             0,
113             NULL,&pWLAN_AVAILABLE_NETWORK_LIST);
114         if(dwResult!=ERROR_SUCCESS)
115         {
116             printf("WlanGetAvailableNetworkListfailedwitherror:%lu\n",dwResult);
117             WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST);
118             return-1;
119         }
120         //connectawlan
121         LPCWSTRprofileXml;
122         std::wstringstrHead=
123             L"<?xmlversion=\"1.0\"encoding=\"US-ASCII\"?>\
124              <WLANProfilexmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\
125              <name>SampleWPA2PSK</name>\
126              <SSIDConfig>\
127              <SSID>\
128              <name>CJLU</name>\
129              </SSID>\
130              </SSIDConfig>\
131              <connectionType>ESS</connectionType>\
132              <connectionMode>auto</connectionMode>\
133              <autoSwitch>false</autoSwitch>\
134              <MSM>\
135              <security>\
136              <authEncryption>\
137              <authentication>WPA2PSK</authentication>\
138              <encryption>AES</encryption>\
139              <useOneX>false</useOneX>\
140              </authEncryption>\
141              <sharedKey>\
142              <keyType>passPhrase</keyType>\
143              <protected>false</protected>\
144              <keyMaterial>5566778899</keyMaterial>\
145              </sharedKey>\
146              </security>\
147              </MSM>\
148              </WLANProfile>";
149         profileXml=strHead.c_str();
150         WLAN_REASON_CODEWlanreason;
151         //如果<connectionMode>auto</connectionMode>,为自动连接,则下面的一步可以连接上无线
152         dwResult=WlanSetProfile(hClient,
153             &(pIfList->InterfaceInfo[0].InterfaceGuid),
154             0,profileXml,NULL,TRUE,NULL,&Wlanreason);
155         if(ERROR_SUCCESS!=dwResult)
156         {
157             printf("wlansetprofilefailed%lu.\r\n",dwResult);
158         }
159         //删除无线配置文件
160         /*
161         LPCWSTRprofileName;
162         LPCWSTRnewProfileName;
163         std::wstringstrprofileName=L"SampleWPA2PSK";
164         std::wstringstrNewProfileName=L"test";
165         profileName=strprofileName.c_str();
166         newProfileName=strNewProfileName.c_str();
167         dwResult=WlanDeleteProfile(hClient,
168         &(pIfList->InterfaceInfo[0].InterfaceGuid),
169         profileName,NULL);
170         if(ERROR_SUCCESS!=dwResult)
171         {
172         printf("wlandeleteprofilefailed%lu.\r\n",dwResult);
173         }
174         */
175         /*
176         //重命名无线配置文件,其实只是新建了一个配置文件,并无重命名(更改了wlanapi.h,将此函数换了条件编译位置)
177         dwResult=WlanRenameProfile(hClient,
178         &(pIfList->InterfaceInfo[0].InterfaceGuid),
179         profileName,
180         newProfileName,
181         NULL
182         );
183         if(ERROR_SUCCESS!=dwResult)
184         {
185         printf("wlanRenameprofilefailed%lu.\r\n",dwResult);
186         }
187         */
188         //网卡关闭和开启(关闭了,就开不了了,除非获取了InterfaceGuid
189         //然后可以关闭再开启。用fn+F2手动开启)
190         WLAN_PHY_RADIO_STATEstate;
191         state.dwPhyIndex=0;
192         state.dot11SoftwareRadioState=dot11_radio_state_on;
193         PVOIDpData=&state;
194         dwResult=WlanSetInterface(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,
195             wlan_intf_opcode_radio_state,sizeof(WLAN_PHY_RADIO_STATE),pData,NULL);
196         if(dwResult!=ERROR_SUCCESS)
197         {
198             wprintf(L"setstatefailed!erris%d\n",dwResult);
199         }
200     }
201     dwResult=WlanCloseHandle(hClient,NULL);
202     if(dwResult!=ERROR_SUCCESS)
203     {
204         wprintf(L"WlanCloseHandlefailed%lu.\r\n",dwResult);
205     }
206     listenStatus();
207     if(pIfList!=NULL){
208         WlanFreeMemory(pIfList);
209         pIfList=NULL;
210     }
211     return0;
212 }

 

posted @ 2019-02-27 16:52  wuyuan2011woaini  阅读(2966)  评论(0编辑  收藏  举报