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>
Value Description
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\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. printf("wlandeleteprofilefailed%lu.\r\n",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.\r\n",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\n",dwResult);  
  195. }  
  196. }  
  197. dwResult=WlanCloseHandle(hClient,NULL);  
  198. if(dwResult!=ERROR_SUCCESS)  
  199. {  
  200. wprintf(L"WlanCloseHandlefailed%lu.\r\n",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/45954309node

相關文章
相關標籤/搜索