原文链接地址: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. jenkins自动打包部署linux

    需要用到2个插件. git parameter:用于参数化构建时选择分支. Publish Over SSH:用于上传jar包和操作tomcat 1.先在系统设置添加要连接的linux服务器,使用用户 ...

  2. 八、EnterpriseFrameWork框架基础功能之自定义报表

    本章写关于框架中的“自定义报表”,类似上章“字典管理”也是三部分功能组成,包括配置报表.对报表按角色授权.查看报表:其核心思想就是实现新增一个报表而不用修改程序代码.不用升级,只需要编写一个存储过程, ...

  3. 在腾讯云上安装mysql遇到的问题

    卸载mysql: 1.sudo apt-get autoremove --purge mysql-server-5.5 5.5 是数据库版本, mysql -v 显示版本信息 2.sudo apt-g ...

  4. JDBC处理大数据

    1.处理大文本 package com.demo; import java.io.File; import java.io.FileNotFoundException; import java.io. ...

  5. selenium,unittest——下拉菜单操作,百度账号设置修改

    #encoding=utf-8from selenium import webdriverimport time,unittest, re,sysfrom HTMLTestRunner import ...

  6. Windows下MongoDB优化及问题处理

    1.MongoDB 服务器CPU占用100% 给Mongodb对应数据库中的表建立索引,这里我采用使用工具:NoSQL Manager for MongoDB 直接在表的属性栏,选择Indexes,右 ...

  7. Centos安装Python3(自带pip和setuptools)

    安装zlib相关依赖 解决zipimport.ZipImportError: can't decompress data和pip3 ssl证书问题 sudo yum -y install zlib* ...

  8. Cuteftp连接虚拟机Centos7

    使用Centos7虚拟机时,想要从主机传一些文件到虚拟机,需要使用FTP传输,在主机上装上的CuteFTP的软件,对虚拟机进行配置. 1,首先,要保证虚拟机能够上网 一般装好虚拟机后,只要主机连了网, ...

  9. Python常用模块之VideoCapture

    官方网址:http://videocapture.sourceforge.net/   功能介绍: VideoCapture是Win32版Python的一个扩展,可以访问视频采集设备(如USB摄像头) ...

  10. ES6的新特性(7)——函数的扩展

    函数的扩展 函数参数的默认值 基本用法 ES6 之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console ...