原文链接地址: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[无线配置文件例程]

 <?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>
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=;
DWORDdwCurVersion=;
DWORDdwResult=;
intiRet=;
WCHARGuidString[]={};
//ListenthestatusoftheAPyouconnected.
while(){
Sleep();
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=;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==)
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=;
DWORDdwCurVersion=;
DWORDdwResult=;
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[].InterfaceGuid,NULL);//DISCONNECTFIRST
if(dwResult!=ERROR_SUCCESS)
{
printf("WlanDisconnectfailedwitherror:%lu\n",dwResult);
return-;
}
//retrievethelistofavailablenetworksonawirelessLANinterface.
PWLAN_AVAILABLE_NETWORK_LISTpWLAN_AVAILABLE_NETWORK_LIST=NULL;
dwResult=WlanGetAvailableNetworkList(hClient,&pIfList->InterfaceInfo[].InterfaceGuid,
,
NULL,&pWLAN_AVAILABLE_NETWORK_LIST);
if(dwResult!=ERROR_SUCCESS)
{
printf("WlanGetAvailableNetworkListfailedwitherror:%lu\n",dwResult);
WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST);
return-;
}
//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></keyMaterial>\
</sharedKey>\
</security>\
</MSM>\
</WLANProfile>";
profileXml=strHead.c_str();
WLAN_REASON_CODEWlanreason;
//如果<connectionMode>auto</connectionMode>,为自动连接,则下面的一步可以连接上无线
dwResult=WlanSetProfile(hClient,
&(pIfList->InterfaceInfo[].InterfaceGuid),
,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=;
state.dot11SoftwareRadioState=dot11_radio_state_on;
PVOIDpData=&state;
dwResult=WlanSetInterface(hClient,&pIfList->InterfaceInfo[].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;
}

C++操作Windows WIFI的更多相关文章

  1. winreg操作windows注册表详解示例

    #coding:utf-8 #=====================================================================#=====本程序演示了WINR ...

  2. C语言操作WINDOWS系统存储区数字证书相关函数详解及实例

     C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...

  3. C# 操作windows服务[启动、停止、卸载、安装]

    主要宗旨:不已命令形式操作windows服务 static void Main(string[] args) { var path = @"E:\开发辅助项目\WCF\WCF.Test\WC ...

  4. 全键盘操作Windows

    计算机机用户在使用计算机的时候,是用键盘多一点?还是用鼠标多一点?如果是专业打字员,应该会说他使用键盘多一点,除此之外,多数人都会告诉你,他已经离不开鼠标了,没有鼠标,就不会操作电脑.   如果某一天 ...

  5. C#通过SC命令和静态公共类来操作Windows服务

    调用的Windows服务应用程序网址:http://www.cnblogs.com/pingming/p/5115304.html 一.引用 二.公共静态类:可以单独放到类库里 using Syste ...

  6. cmd命令行和bat批处理操作windows服务(转载)

    一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32srv ...

  7. (转)Python 操作 Windows 粘贴板

    转自: http://outofmemory.cn/code-snippet/3939/Python-operation-Windows-niantie-board Python 操作 Windows ...

  8. C#使用DirectoryEntry类操作Windows帐户

    1.创建windows帐户 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /// <summary> /// 创建Windows帐户 /// </summa ...

  9. [经验分享]C# 操作Windows系统计划任务

    背景:我做了一个事情是要自己提前创建好很多要定时执行的任务,在我不在的时候自动执行这些程序,以保证我的工作能无人值守,那么我就需要建立系统计划任务来帮我完成这件事情,当然用脑子想想如何实现,很简单,每 ...

随机推荐

  1. 八月暑期福利,10本Python热门书籍免费送!

    八月第一周,网易云社区联合博文视点为大家带来Python专场送书福利,10本关于Python的书籍内容涉及Python入门.绝技.开发.数据分析.深度学习.量化投资等.以下为书籍简介,送书福利请见文末 ...

  2. Spark 序列化问题

    在Spark应用开发中,很容易出现如下报错: org.apache.spark.SparkException: Task not serializable at org.apache.spark.ut ...

  3. [转]关于PreparedStatement.addBatch()方法

    Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...

  4. MindMaster安装教程以及激活破解教程

    原文地址:https://www.jianshu.com/p/16d2fc7d8e45 第一.激活必须首先断网 第二.运行安装程序,安装完成后先不要打开 第三.把Cracks文件夹下的文件复制到软件安 ...

  5. Liunx expect 基础

    a script for study except #!/usr/bin/expect 声明文件内的语法使用 expect 的语法来执行. send send: 向进程发送字符串,用于模拟用户的输入. ...

  6. RNN: Feed Forward, Back Propagation Through Time and Truncated Backpropagation Through Time

    原创作品,转载请注明出处哦~ 了解RNN的前向.后向传播算法的推导原理是非常重要的,这样, 1. 才会选择正确的激活函数: 2. 才会选择合适的前向传播的timesteps数和后向传播的timeste ...

  7. 译 - Cassandra 数据建模的基本规则

    Basic Rules of Cassandra Data Modeling 原文地址:http://www.datastax.com/dev/blog/basic-rules-of-cassandr ...

  8. Github二次学习

    作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正. 本节课视频内容:https://www ...

  9. 用P4对数据平面进行编程

    引言 SDN架构强调了对控制平面的可编程,数据平面只负责转发,导致数据平面很大程度上受制于功能固定的包处理硬件. P4语言的特性: 目标无关性:P4语言不受制于具体设备,所有可编程芯片都可以使用P4编 ...

  10. UVA 10328 - Coin Toss dp+大数

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...