QT---Native Wifi functions 应用(WiFi有密码连接)

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

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

    

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

    例如常见的路由器,安全类型为WPA2-PSK的xml配置文件如下(有密码):
    
<?xml version="1.0" encoding="US-ASCII"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>SampleWPA2PSK</name>
    <SSIDConfig>
        <SSID>
            <name>SampleWPA2PSK</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <autoSwitch>false</autoSwitch>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
<sharedKey>
    <keyType>passPhrase</keyType>
    <protected>false</protected>
    <keyMaterial> <!-- insert key here --> </keyMaterial>
</sharedKey>
        </security>
    </MSM>
</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>
[cpp] view plain copy
 
  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 ",dwResult);  
  25. return1;  
  26. }  
  27. //获取无线网卡列表  
  28. dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);  
  29. if(dwResult!=ERROR_SUCCESS){  
  30. wprintf(L"WlanEnumInterfacesfailedwitherror:%u ",dwResult);  
  31. return1;  
  32. }else{  
  33. wprintf(L"NumEntries:%lu ",pIfList->dwNumberOfItems);  
  34. wprintf(L"CurrentIndex:%lu ",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]: %lu ",i,i);  
  39. iRet=StringFromGUID2(pIfInfo->InterfaceGuid,(LPOLESTR)&GuidString,  
  40. sizeof(GuidString)/sizeof(*GuidString));  
  41. if(iRet==0)  
  42. wprintf(L"StringFromGUID2failed ");  
  43. else{  
  44. wprintf(L"InterfaceGUID[%d]:%S ",i,GuidString);  
  45. }  
  46. wprintf(L"InterfaceDescription[%d]:%S",i,  
  47. pIfInfo->strInterfaceDescription);  
  48. wprintf(L" ");  
  49. wprintf(L"InterfaceState[%d]: ",i);  
  50. switch(pIfInfo->isState){  
  51. casewlan_interface_state_not_ready:  
  52. wprintf(L"Notready ");  
  53. break;  
  54. casewlan_interface_state_connected:  
  55. wprintf(L"Connected ");  
  56. break;  
  57. casewlan_interface_state_ad_hoc_network_formed:  
  58. wprintf(L"Firstnodeinaadhocnetwork ");  
  59. break;  
  60. casewlan_interface_state_disconnecting:  
  61. wprintf(L"Disconnecting ");  
  62. break;  
  63. casewlan_interface_state_disconnected:  
  64. wprintf(L"Notconnected ");  
  65. break;  
  66. casewlan_interface_state_associating:  
  67. wprintf(L"Attemptingtoassociatewithanetwork ");  
  68. break;  
  69. casewlan_interface_state_discovering:  
  70. wprintf(L"Autoconfigurationisdiscoveringsettingsforthenetwork ");  
  71. break;  
  72. casewlan_interface_state_authenticating:  
  73. wprintf(L"Inprocessofauthenticating ");  
  74. break;  
  75. default:  
  76. wprintf(L"Unknownstate%ld ",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 ",dwResult);  
  94. return1;  
  95. }  
  96. //enumeratesallofthewirelessLANinterfacescurrentlyenabledonthelocalcomputer.  
  97. dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);  
  98. if(dwResult!=ERROR_SUCCESS){  
  99. wprintf(L"WlanEnumInterfacesfailedwitherror:%u ",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 ",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 ",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. ",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. printf("wlandeleteprofilefailed%lu. ",dwResult); 
  172. */  
  173. /* 
  174. //重命名无线配置文件,其实只是新建了一个配置文件,并无重命名(更改了wlanapi.h,将此函数换了条件编译位置) 
  175. dwResult=WlanRenameProfile(hClient, 
  176. &(pIfList->InterfaceInfo[0].InterfaceGuid), 
  177. profileName, 
  178. newProfileName, 
  179. NULL 
  180. ); 
  181. if(ERROR_SUCCESS!=dwResult) 
  182. printf("wlanRenameprofilefailed%lu. ",dwResult); 
  183. */  
  184. //网卡关闭和开启(关闭了,就开不了了,除非获取了InterfaceGuid  
  185. //然后可以关闭再开启。用fn+F2手动开启)  
  186. WLAN_PHY_RADIO_STATEstate;  
  187. state.dwPhyIndex=0;  
  188. state.dot11SoftwareRadioState=dot11_radio_state_on;  
  189. PVOIDpData=&state;  
  190. dwResult=WlanSetInterface(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,  
  191. wlan_intf_opcode_radio_state,sizeof(WLAN_PHY_RADIO_STATE),pData,NULL);  
  192. if(dwResult!=ERROR_SUCCESS)  
  193. {  
  194. wprintf(L"setstatefailed!erris%d ",dwResult);  
  195. }  
  196. }  
  197. dwResult=WlanCloseHandle(hClient,NULL);  
  198. if(dwResult!=ERROR_SUCCESS)  
  199. {  
  200. wprintf(L"WlanCloseHandlefailed%lu. ",dwResult);  
  201. }  
  202. listenStatus();  
  203. if(pIfList!=NULL){  
  204. WlanFreeMemory(pIfList);  
  205. pIfList=NULL;  
  206. }  
  207. return0;  
  208. }  

http://blog.csdn.net/freeape/article/details/45954309

原文地址:https://www.cnblogs.com/findumars/p/6294156.html