实现功能
无线网卡列表
无线热点扫面
无线连接(有密码,配置文件连接方式)
无线断开
重命名本地无线名(两种方式)
删除无线配置文件
开启和关闭无线网卡
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 ",dwResult);
- return1;
- }
- //获取无线网卡列表
- dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanEnumInterfacesfailedwitherror:%u ",dwResult);
- return1;
- }else{
- wprintf(L"NumEntries:%lu ",pIfList->dwNumberOfItems);
- wprintf(L"CurrentIndex:%lu ",pIfList->dwIndex);
- inti;
- for(i=0;i<(int)pIfList->dwNumberOfItems;i++){
- pIfInfo=(WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i];
- wprintf(L"InterfaceIndex[%u]: %lu ",i,i);
- iRet=StringFromGUID2(pIfInfo->InterfaceGuid,(LPOLESTR)&GuidString,
- sizeof(GuidString)/sizeof(*GuidString));
- if(iRet==0)
- wprintf(L"StringFromGUID2failed ");
- else{
- wprintf(L"InterfaceGUID[%d]:%S ",i,GuidString);
- }
- wprintf(L"InterfaceDescription[%d]:%S",i,
- pIfInfo->strInterfaceDescription);
- wprintf(L" ");
- wprintf(L"InterfaceState[%d]: ",i);
- switch(pIfInfo->isState){
- casewlan_interface_state_not_ready:
- wprintf(L"Notready ");
- break;
- casewlan_interface_state_connected:
- wprintf(L"Connected ");
- break;
- casewlan_interface_state_ad_hoc_network_formed:
- wprintf(L"Firstnodeinaadhocnetwork ");
- break;
- casewlan_interface_state_disconnecting:
- wprintf(L"Disconnecting ");
- break;
- casewlan_interface_state_disconnected:
- wprintf(L"Notconnected ");
- break;
- casewlan_interface_state_associating:
- wprintf(L"Attemptingtoassociatewithanetwork ");
- break;
- casewlan_interface_state_discovering:
- wprintf(L"Autoconfigurationisdiscoveringsettingsforthenetwork ");
- break;
- casewlan_interface_state_authenticating:
- wprintf(L"Inprocessofauthenticating ");
- break;
- default:
- wprintf(L"Unknownstate%ld ",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 ",dwResult);
- return1;
- }
- //enumeratesallofthewirelessLANinterfacescurrentlyenabledonthelocalcomputer.
- dwResult=WlanEnumInterfaces(hClient,NULL,&pIfList);
- if(dwResult!=ERROR_SUCCESS){
- wprintf(L"WlanEnumInterfacesfailedwitherror:%u ",dwResult);
- return1;
- }else{
- //disconnectsaninterfacefromitscurrentnetwork.
- dwResult=WlanDisconnect(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,NULL);//DISCONNECTFIRST
- if(dwResult!=ERROR_SUCCESS)
- {
- printf("WlanDisconnectfailedwitherror:%lu ",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 ",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. ",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. ",dwResult);
- }
- */
- /*
- //重命名无线配置文件,其实只是新建了一个配置文件,并无重命名(更改了wlanapi.h,将此函数换了条件编译位置)
- dwResult=WlanRenameProfile(hClient,
- &(pIfList->InterfaceInfo[0].InterfaceGuid),
- profileName,
- newProfileName,
- NULL
- );
- if(ERROR_SUCCESS!=dwResult)
- {
- printf("wlanRenameprofilefailed%lu. ",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 ",dwResult);
- }
- }
- dwResult=WlanCloseHandle(hClient,NULL);
- if(dwResult!=ERROR_SUCCESS)
- {
- wprintf(L"WlanCloseHandlefailed%lu. ",dwResult);
- }
- listenStatus();
- if(pIfList!=NULL){
- WlanFreeMemory(pIfList);
- pIfList=NULL;
- }
- return0;
- }
http://blog.csdn.net/freeape/article/details/45954309