實現功能
無線網卡列表
無線熱點掃面
無線連接(有密碼,配置文件連接方式)
無線斷開
重命名本地無線名(兩種方式)
刪除無線配置文件
開啟和關閉無線網卡
Native Wifi 簡介
是提供給軟件開發者來開發windows 無線管理的一系列API。編程人員可以通過這些函數來進行相關的無線管理,當然我們還可以通過netsh終端命令來管理,這對於非編程人員就可以簡單的實現,具體可以查閱相關資料去了解。
API如下:
- WLAN_NOTIFICATION_CALLBACK
- WlanAllocateMemory
- WlanCloseHandle
- WlanConnect
- WlanDeleteProfile
- WlanDisconnect
- WlanEnumInterfaces
- WlanExtractPsdIEDataList
- WlanFreeMemory
- WlanGetAvailableNetworkList
- WlanGetFilterList
- WlanGetInterfaceCapability
- WlanGetNetworkBssList
- WlanGetProfile
- WlanGetProfileCustomUserData
- WlanGetProfileList
- WlanGetSecuritySettings
- WlanHostedNetworkForceStart
- WlanHostedNetworkForceStop
- WlanHostedNetworkInitSettings
- WlanHostedNetworkQueryProperty
- WlanHostedNetworkQuerySecondaryKey
- WlanHostedNetworkQueryStatus
- WlanHostedNetworkRefreshSecuritySettings
- WlanHostedNetworkSetProperty
- WlanHostedNetworkSetSecondaryKey
- WlanHostedNetworkStartUsing
- WlanHostedNetworkStopUsing
- WlanIhvControl
- WlanOpenHandle
- WlanQueryAutoConfigParameter
- WlanQueryInterface
- WlanReasonCodeToString
- WlanRegisterNotification
- WlanRegisterVirtualStationNotification
- WlanRenameProfile
- WlanSaveTemporaryProfile
- WlanScan
- WlanSetAutoConfigParameter
- WlanSetFilterList
- WlanSetInterface
- WlanSetProfile
- WlanSetProfileCustomUserData
- WlanSetProfileEapUserData
- WlanSetProfileEapXmlUserData
- WlanSetProfileList
- WlanSetProfilePosition
- WlanSetPsdIeDataList
- WlanSetSecuritySettings
無線的連接相關知識
從windows的無線網絡屬性設置窗口來對比,在API編程中,同樣有個配置文件來設置這些屬性的,那就是profile文件,通過編寫xml文件來設置相關屬性。
WLAN_profile Schema Elements [xml配置文件編寫格式]:
- WLANProfile
- name (WLANProfile)
- SSIDConfig (WLANProfile)
- connectionType (WLANProfile)
- connectionMode (WLANProfile)
- autoSwitch (WLANProfile)
- MSM (WLANProfile)
- IHV (WLANProfile)
Wireless Profile Samples[無線配置文件例程]
- Bootstrap Profile Sample
- FIPS Profile Sample
- Non-Broadcast Profile Sample
- Single Sign-On Profile Sample
- WPA-Enterprise with PEAP-MSCHAPv2 Profile Sample
- WPA-Enterprise with TLS Profile Sample
- WPA-Personal Profile Sample
- WPA2-Enterprise with PEAP-MSCHAPv2 Profile Sample
- WPA2-Enterprise with TLS Profile Sample
- WPA2-Personal Profile Sample
例如常見的路由器,安全類型為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>
具體功能編程實現(QTCtreator 5.xx)
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.
.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};
- //ListenthestatusoftheAPyouconnected.
- 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;
- //opensaconnectiontotheserver.
- dwResult=WlanOpenHandle(dwMaxClient,NULL,&dwCurVersion,&hClient);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanOpenHandlefailedwitherror:%u\n",dwResult);
- return1;
- }
- //enumeratesallofthewirelessLANinterfacescurrentlyenabledonthelocalcomputer.
- dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanEnumInterfacesfailedwitherror:%u\n",dwResult);
- return1;
- }else{
- //disconnectsaninterfacefromitscurrentnetwork.
- dwResult=WlanDisconnect(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,NULL);//DISCONNECTFIRST
- if(dwResult!=ERROR_SUCCESS)
- {
- printf("WlanDisconnectfailedwitherror:%lu\n",dwResult);
- return-1;
- }
- //retrievethelistofavailablenetworksonawirelessLANinterface.
- 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;
- }
- //connectawlan
- 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;
- //如果<connectionMode>auto</connectionMode>,為自動連接,則下面的一步可以連接上無線
- dwResult=WlanSetProfile(hClient,
- &(pIfList->InterfaceInfo[0].InterfaceGuid),
- 0,profileXml,NULL,TRUE,NULL,&Wlanreason);
- if(ERROR_SUCCESS!=dwResult)
- {
- printf("wlansetprofilefailed%lu.\r\n",dwResult);
- }
- //刪除無線配置文件
- /*
- LPCWSTRprofileName;
- LPCWSTRnewProfileName;
- std::wstringstrprofileName=L"SampleWPA2PSK";
- std::wstringstrNewProfileName=L"test";
- profileName=strprofileName.c_str();
- newProfileName=strNewProfileName.c_str();
- dwResult=WlanDeleteProfile(hClient,
- &(pIfList->InterfaceInfo[0].InterfaceGuid),
- profileName,NULL);
- if(ERROR_SUCCESS!=dwResult)
- {
- printf("wlandeleteprofilefailed%lu.\r\n",dwResult);
- }
- */
- /*
- //重命名無線配置文件,其實只是新建了一個配置文件,並無重命名(更改了wlanapi.h,將此函數換了條件編譯位置)
- dwResult=WlanRenameProfile(hClient,
- &(pIfList->InterfaceInfo[0].InterfaceGuid),
- profileName,
- newProfileName,
- NULL
- );
- if(ERROR_SUCCESS!=dwResult)
- {
- printf("wlanRenameprofilefailed%lu.\r\n",dwResult);
- }
- */
- //網卡關閉和開啟(關閉了,就開不了了,除非獲取了InterfaceGuid
- //然后可以關閉再開啟。用fn+F2手動開啟)
- 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/45954309
