原文链接地址: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. Flask开发环境搭建

    基础准备 Python 3.6.5 Conda Visual Studio Code 虚拟环境 创建虚拟环境 conda create -n flask 激活虚拟环境 activate flask 关 ...

  2. flask ssti python2和python3 注入总结和区别

    总结一下flask ssti的注入语句 代码 import uuid from flask import Flask, request, make_response, session,render_t ...

  3. Centos7.2部署saltstack

    原文发表于cu:2016-06-23 参考文档: Saltstack安装文档:https://repo.saltstack.com/#rhel saltstack的安装与简单配置,应用. 一.环境 S ...

  4. 如何让QT程序以管理员权限运行(UAC)

    方案一:(仅适用于使用msvc编译器) 在PRO文件中添加一行指令即可, QMAKE_LFLAGS += /MANIFESTUAC:"level='requireAdministrator' ...

  5. Immutable 常用API简介

    本文主要整理了Immutable.js常用API的使用. Immutable 是什么? 关于Immutable的定义,官方文档是这样说的: Immutable data encourages pure ...

  6. Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)

    A bracket(括号) sequence is a string containing only characters "(" and ")".A regu ...

  7. LeetCode 289. Game of Life (C++)

    题目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a ce ...

  8. 论文爬取 & 词频统计2.0

    一.Github地址      课程项目要求    队友博客 二.具体分工 031602225 林煌伟 :负责C++部分主要功能函数的编写,算法的设计以及改进优化 031602230 卢恺翔 : 爬虫 ...

  9. MMU 和 MPU的区别

    S3C2440里面带的是MMU,而现在流行的Cortex-M3/4 里面带的是MPU. MMU vs MPU 内存是现代计算机最重要的组件之一.因此,它的内容不能被任何错误的应用所篡改.这个功能可以通 ...

  10. 关于解决java读取excel文件遇空行抛空指针的问题 !

    关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...