實現功能
無線網卡列表
無線熱點掃面
無線鏈接(有密碼,配置文件鏈接方式)
無線斷開
重命名本地無線名(兩種方式)
刪除無線配置文件
開啓和關閉無線網卡
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>
注:
【加密類型設置】
<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>
- 程序實現(終端打印信息)
- #include<windows.h>
- #include<wlanapi.h>
- #include<string>
- #include<stdio.h>
-
- intlistenStatus()
- {
- HANDLEhClient=NULL;
- DWORDdwMaxClient=2;
- DWORDdwCurVersion=0;
- DWORDdwResult=0;
- intiRet=0;
- WCHARGuidString[39]={0};
- while(1){
- Sleep(5000);
- PWLAN_INTERFACE_INFO_LISTpIfList=NULL;
- PWLAN_INTERFACE_INFOpIfInfo=NULL;
-
- dwResult=WlanOpenHandle(dwMaxClient,NULL,&dwCurVersion,&hClient);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanOpenHandlefailedwitherror:%u\n",dwResult);
- return1;
- }
- dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanEnumInterfacesfailedwitherror:%u\n",dwResult);
- return1;
- }else{
- wprintf(L"NumEntries:%lu\n",pIfList->dwNumberOfItems);
- wprintf(L"CurrentIndex:%lu\n\n",pIfList->dwIndex);
- inti;
- for(i=0;i<(int)pIfList->dwNumberOfItems;i++){
- pIfInfo=(WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i];
- wprintf(L"InterfaceIndex[%u]:\t%lu\n",i,i);
- iRet=StringFromGUID2(pIfInfo->InterfaceGuid,(LPOLESTR)&GuidString,
- sizeof(GuidString)/sizeof(*GuidString));
- if(iRet==0)
- wprintf(L"StringFromGUID2failed\n");
- else{
- wprintf(L"InterfaceGUID[%d]:%S\n",i,GuidString);
- }
- wprintf(L"InterfaceDescription[%d]:%S",i,
- pIfInfo->strInterfaceDescription);
- wprintf(L"\n");
- wprintf(L"InterfaceState[%d]:\t",i);
- switch(pIfInfo->isState){
- casewlan_interface_state_not_ready:
- wprintf(L"Notready\n");
- break;
- casewlan_interface_state_connected:
- wprintf(L"Connected\n");
- break;
- casewlan_interface_state_ad_hoc_network_formed:
- wprintf(L"Firstnodeinaadhocnetwork\n");
- break;
- casewlan_interface_state_disconnecting:
- wprintf(L"Disconnecting\n");
- break;
- casewlan_interface_state_disconnected:
- wprintf(L"Notconnected\n");
- break;
- casewlan_interface_state_associating:
- wprintf(L"Attemptingtoassociatewithanetwork\n");
- break;
- casewlan_interface_state_discovering:
- wprintf(L"Autoconfigurationisdiscoveringsettingsforthenetwork\n");
- break;
- casewlan_interface_state_authenticating:
- wprintf(L"Inprocessofauthenticating\n");
- break;
- default:
- wprintf(L"Unknownstate%ld\n",pIfInfo->isState);
- break;
- }
- }
- }
- }
- }
- intmain()
- {
- HANDLEhClient=NULL;
- DWORDdwMaxClient=2;
- DWORDdwCurVersion=0;
- DWORDdwResult=0;
- PWLAN_INTERFACE_INFO_LISTpIfList=NULL;
- dwResult=WlanOpenHandle(dwMaxClient,NULL,&dwCurVersion,&hClient);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanOpenHandlefailedwitherror:%u\n",dwResult);
- return1;
- }
- dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanEnumInterfacesfailedwitherror:%u\n",dwResult);
- return1;
- }else{
- dwResult=WlanDisconnect(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,NULL);
- if(dwResult!=ERROR_SUCCESS)
- {
- printf("WlanDisconnectfailedwitherror:%lu\n",dwResult);
- return-1;
- }
- PWLAN_AVAILABLE_NETWORK_LISTpWLAN_AVAILABLE_NETWORK_LIST=NULL;
- dwResult=WlanGetAvailableNetworkList(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,
- 0,
- NULL,&pWLAN_AVAILABLE_NETWORK_LIST);
- if(dwResult!=ERROR_SUCCESS)
- {
- printf("WlanGetAvailableNetworkListfailedwitherror:%lu\n",dwResult);
- WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST);
- return-1;
- }
- LPCWSTRprofileXml;
- std::wstringstrHead=
- L"<?xmlversion=\"1.0\"encoding=\"US-ASCII\"?>\
- <WLANProfilexmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\
- <name>SampleWPA2PSK</name>\
- <SSIDConfig>\
- <SSID>\
- <name>CJLU</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>5566778899</keyMaterial>\
- </sharedKey>\
- </security>\
- </MSM>\
- </WLANProfile>";
- profileXml=strHead.c_str();
- WLAN_REASON_CODEWlanreason;
- dwResult=WlanSetProfile(hClient,
- &(pIfList->InterfaceInfo[0].InterfaceGuid),
- 0,profileXml,NULL,TRUE,NULL,&Wlanreason);
- if(ERROR_SUCCESS!=dwResult)
- {
- printf("wlansetprofilefailed%lu.\r\n",dwResult);
- }
- WLAN_PHY_RADIO_STATEstate;
- state.dwPhyIndex=0;
- state.dot11SoftwareRadioState=dot11_radio_state_on;
- PVOIDpData=&state;
- dwResult=WlanSetInterface(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,
- wlan_intf_opcode_radio_state,sizeof(WLAN_PHY_RADIO_STATE),pData,NULL);
- if(dwResult!=ERROR_SUCCESS)
- {
- wprintf(L"setstatefailed!erris%d\n",dwResult);
- }
- }
- dwResult=WlanCloseHandle(hClient,NULL);
- if(dwResult!=ERROR_SUCCESS)
- {
- wprintf(L"WlanCloseHandlefailed%lu.\r\n",dwResult);
- }
- listenStatus();
- if(pIfList!=NULL){
- WlanFreeMemory(pIfList);
- pIfList=NULL;
- }
- return0;
- }
http://blog.csdn.net/freeape/article/details/45954309node