博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++操作Windows WIFI
阅读量:5366 次
发布时间:2019-06-15

本文共 9584 字,大约阅读时间需要 31 分钟。

原文链接地址:https://blog.csdn.net/just_do_1122/article/details/78031024

实现功能
    无线网卡列表
    无线热点扫面
    无线连接(有密码,配置文件连接方式)
    无线断开
    重命名本地无线名(两种方式)
    删除无线配置文件
    开启和关闭无线网卡
Native Wifi 简介
    是提供给软件开发者来开发windows 无线管理的一系列API。编程人员可以通过这些函数来进行相关的无线管理,当然我们还可以通过netsh终端命令来管理,这对于非编程人员就可以简单的实现,具体可以查阅相关资料去了解。
    API如下:
无线的连接相关知识
 

 

 

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

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

    

Wireless Profile Samples[无线配置文件例程]

1 
2
3
SampleWPA2PSK
4
5
6
SampleWPA2PSK
7
8
9
ESS
10
auto
11
false
12
13
14
15
WPA2PSK
16
AES
17
false
18
19
20
passPhrase
21
false
22
23
24
25
26
注:
【加密类型设置】
AES
【安全类型设置】
WPAPSK
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
#include
1 程序实现(终端打印信息)  2 #include
3 #include
4 #include
5 #include
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);//DISCONNECTFIRST104 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 //connectawlan121 LPCWSTRprofileXml;122 std::wstringstrHead=123 L"
\124
\125
SampleWPA2PSK
\126
\127
\128
CJLU
\129
\130
\131
ESS
\132
auto
\133
false
\134
\135
\136
\137
WPA2PSK
\138
AES
\139
false
\140
\141
\142
passPhrase
\143
false
\144
5566778899
\145
\146
\147
\148 ";149 profileXml=strHead.c_str();150 WLAN_REASON_CODEWlanreason;151 //如果
auto
,为自动连接,则下面的一步可以连接上无线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 {172 printf("wlandeleteprofilefailed%lu.\r\n",dwResult);173 }174 */175 /*176 //重命名无线配置文件,其实只是新建了一个配置文件,并无重命名(更改了wlanapi.h,将此函数换了条件编译位置)177 dwResult=WlanRenameProfile(hClient,178 &(pIfList->InterfaceInfo[0].InterfaceGuid),179 profileName,180 newProfileName,181 NULL182 );183 if(ERROR_SUCCESS!=dwResult)184 {185 printf("wlanRenameprofilefailed%lu.\r\n",dwResult);186 }187 */188 //网卡关闭和开启(关闭了,就开不了了,除非获取了InterfaceGuid189 //然后可以关闭再开启。用fn+F2手动开启)190 WLAN_PHY_RADIO_STATEstate;191 state.dwPhyIndex=0;192 state.dot11SoftwareRadioState=dot11_radio_state_on;193 PVOIDpData=&state;194 dwResult=WlanSetInterface(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid,195 wlan_intf_opcode_radio_state,sizeof(WLAN_PHY_RADIO_STATE),pData,NULL);196 if(dwResult!=ERROR_SUCCESS)197 {198 wprintf(L"setstatefailed!erris%d\n",dwResult);199 }200 }201 dwResult=WlanCloseHandle(hClient,NULL);202 if(dwResult!=ERROR_SUCCESS)203 {204 wprintf(L"WlanCloseHandlefailed%lu.\r\n",dwResult);205 }206 listenStatus();207 if(pIfList!=NULL){208 WlanFreeMemory(pIfList);209 pIfList=NULL;210 }211 return0;212 }

 

转载于:https://www.cnblogs.com/wuyuan2011woaini/p/10444820.html

你可能感兴趣的文章
第四周作业
查看>>
一、HTML基础
查看>>
蓝牙进阶之路 (002) - HC-05与HC-06的AT指令的区别(转)
查看>>
mysql的limit经典用法及优化
查看>>
C#后台程序与HTML页面中JS方法互调
查看>>
mysql 同一个表中 字段a 的值赋值到字段b
查看>>
linux系统可执行文件添加环境变量使其跨终端和目录执行
查看>>
antiSMASH数据库:微生物次生代谢物合成基因组簇查询和预测
查看>>
UNICODE与ANSI的区别
查看>>
nginx 配置实例
查看>>
Flutter - 创建底部导航栏
查看>>
ASP.NET MVC 教程-MVC简介
查看>>
SQL Server索引 - 聚集索引、非聚集索引、非聚集唯一索引 <第八篇>
查看>>
转载:详解SAP TPM解决方案在快速消费品行业中的应用
查看>>
Android OpenGL ES 开发(N): OpenGL ES 2.0 机型兼容问题整理
查看>>
项目中用到的技术及工具汇总(持续更新)
查看>>
【算法】各种排序算法测试代码
查看>>
HDU 5776 Sum
查看>>
201521123044 《Java程序设计》第9周学习总结
查看>>
winfrom 图片等比例压缩
查看>>