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

来源:互联网 发布:fc2破解版最新域名设置 编辑:程序博客网 时间:2024/05/21 18:32
实现功能
    无线网卡列表
    无线热点扫面
    无线连接(有密码,配置文件连接方式)
    无线断开
    重命名本地无线名(两种方式)
    删除无线配置文件
    开启和关闭无线网卡
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

  • WlanUIEditProfile
无线的连接相关知识


    从windows的无线网络属性设置窗口来对比,在API编程中,同样有个配置文件来设置这些属性的,那就是profile文件,通过编写xml文件来设置相关属性。

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

  • WLANProfile
    • name (WLANProfile)
    • SSIDConfig (WLANProfile)
      • SSID (SSIDConfig)
        • hex (SSID)
        • name (SSID)
      • nonBroadcast (SSIDConfig)
    • connectionType (WLANProfile)
    • connectionMode (WLANProfile)
    • autoSwitch (WLANProfile)
    • MSM (WLANProfile)
      • connectivity (MSM)
        • phyType (connectivity)
      • security (MSM)
        • authEncryption (security)
          • authentication (authEncryption)
          • encryption (authEncryption)
          • useOneX (authEncryption)
          • FIPSMode (authEncryption)
        • sharedKey (security)
          • keyType (sharedKey)
          • protected (sharedKey)
          • keyMaterial (sharedKey)
        • keyIndex (security)
        • PMKCacheMode (security)
        • PMKCacheTTL (security)
        • PMKCacheSize (security)
        • preAuthMode (security)
        • preAuthThrottle (security)
    • IHV (WLANProfile)
      • OUIHeader (IHV)
        • OUI (OUIHeader)
        • type (OUIHeader)
      • connectivity (IHV)
      • security (IHV)
      • useMSOneX (IHV)
    

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>
ValueDescriptionopenOpen 802.11 authentication.sharedShared 802.11 authentication.WPAWPA-Enterprise 802.11 authentication.WPAPSKWPA-Personal 802.11 authentication.WPA2WPA2-Enterprise 802.11 authentication.WPA2PSKWPA2-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};//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);//DISCONNECTFIRSTif(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;}//connectawlanLPCWSTRprofileXml;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;}


0 0