c# 如何使用wlanapi连接电脑到wifi
http://www.codeproject.com/Articles/72105/Manage-WiFi-with-Native-API-WIFI-on-Windows-XP-SP
Introduction
This article will demonstrate the ability to establish and operate a WiFi connection from a C# program using the Wireless LAN API on a Windows XP Embedded SP2 \ Windows XP Pro SP2.
Step 1
The first step was to create a function that returns the WiFi networks available for connection. Using the WLANOpenHandle, you create the client to use for the session:
Wlan.WlanOpenHandle(
Wlan.WLAN_CLIENT_VERSION_XP_SP2, <--- (Wireless LAN API for Windows XP with SP2)
IntPtr.Zero,
ref negotiatedVersion,
ref clientHandle)
Step 2
Then list all the wireless LAN interfaces visible from our PC:
Wlan.WlanEnumInterfaces(
clientHandle, <--- (clientHandle precedentemente istanziato)
IntPtr.Zero,
ref pInterfaceList)
Step 3
Using the pointer to the list of available interfaces, we will populate the structure that will contain all Wlan.WLAN_INTERFACE_INFO_LIST NIC interface information:
Wlan.WLAN_INTERFACE_INFO_LIST interfaceList =
new Wlan.WLAN_INTERFACE_INFO_LIST(pInterfaceList);
Step 4
What we are still missing at this point and guidance of our interface to be passed to the function Wlan.WlanGetAvailableNetworkList, we get through this passage:
Guid InterfaceGuid =
((Wlan.WLAN_INTERFACE_INFO)interfaceList.InterfaceInfo[0]).InterfaceGuid;
Step 5
Now we have all the elements to call the API Wlan.WlanGetAvailableNetworkList; declare that we will return the pointer function and proceed:
IntPtr pAvailableNetworkList = IntPtr.Zero;
Wlan.WlanGetAvailableNetworkList( clientHandle,
InterfaceGuid,
Wlan.WlanGetAvailableNetworkFlags.
IncludeAllManualHiddenProfiles,
IntPtr.Zero,
out ppAvailableNetworkList)
Scroll through the list of connections:
Wlan.WLAN_AVAILABLE_NETWORK_LIST wlanAvailableNetworkList =
new Wlan.WLAN_AVAILABLE_NETWORK_LIST(pAvailableNetworkList);
Wlan.WlanFreeMemory(pAvailableNetworkList);
for (int j = 0; j < wlanAvailableNetworkList.dwNumberOfItems; j++)
{
Wlan.WLAN_AVAILABLE_NETWORK network = wlanAvailableNetworkList.Networks[j];
string SSId = GetStringForSSID(network.dot11Ssid);
if(ssid != "")
{
//Connessione
Wifi_SetProfile(network.dot11DefaultAuthAlgorithm, SSId);
}
}
Depending on the type of connection configured for my Access Point imposed the correct profile and I connect:
static void Wifi_SetProfile(Wlan.DOT11_AUTH_ALGORITHM connType, string sNetSSId)
{
switch (connType)
{
//WEP - OPen
case Wlan.DOT11_AUTH_ALGORITHM.DOT11_AUTH_ALGO_80211_OPEN:
profileXml = string.Format(
"<?xml version=\"1.0\"?>
<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">
<name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig>
<connectionType>ESS</connectionType><MSM><security><authEncryption>
<authentication>open</authentication><encryption>WEP</encryption>
<useOneX>false</useOneX></authEncryption><sharedKey>
<keyType>networkKey</keyType><protected>false</protected>
<keyMaterial>{1}</keyMaterial></sharedKey><keyIndex>0</keyIndex>
</security></MSM></WLANProfile>",
profileName, key); Wlan.WlanSetProfile( clientHandle,
InterfaceGuid,
Wlan.WlanProfileFlags.AllUser,
profileXml,
null,
true,
IntPtr.Zero,
out reasonCode); if(reasonCode==Wlan.WlanReasonCode.Success)
{
//Connect!!!
...
}
else
{
//Return usually 1168 – Error is on profile Xml or on key
}
break; //WPA_PSK
case Wlan.DOT11_AUTH_ALGORITHM.DOT11_AUTH_ALGO_WPA_PSK:
profileXml = string.Format("<?xml version=\"1.0\" encoding=\"US-ASCII\"?>
<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">
<name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig>
<connectionType>ESS</connectionType><connectionMode>auto</connectionMode>
<autoSwitch>false</autoSwitch><MSM><security><authEncryption>
<authentication>WPAPSK</authentication><encryption>TKIP</encryption>
<useOneX>false</useOneX></authEncryption><sharedKey>
<keyType>passPhrase</keyType><protected>false</protected>
<keyMaterial>{1}</keyMaterial></sharedKey></security></MSM></WLANProfile>",
profileName, key); Wlan.WlanSetProfile( clientHandle,
InterfaceGuid,
Wlan.WlanProfileFlags.AllUser,
profileXml,
null,
true,
IntPtr.Zero,
out reasonCode); if(reasonCode == Wlan.WlanReasonCode.Success)
{
//Connect!!!
...
}
else
{
//Return usually 1168 – Error is on profile Xml or on key
}
break;
}
补充参考:
http://stackoverflow.com/questions/6318575/connect-to-a-wireless-network-in-c-sharp
http://msdn.microsoft.com/en-us/library/windows/desktop/aa370032(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa369853(v=vs.85).aspx
c# 如何使用wlanapi连接电脑到wifi的更多相关文章
- 使用cmd查看电脑连接过的wifi密码并将密码发送至指定邮箱(三)
之前,我写了使用cmd查看电脑连接过的wifi密码(二)和使用cmd查看电脑连接过的wifi密码(一)但其中的功能不完善,在本次的更新中新增了更多的功能,其实2018/10/24 就更新完成了,一直拖 ...
- 查看window系统电脑连接过的wifi密码
电脑连接过的wifi都会有痕迹,包括SSID号和密码等信息,借此可以回查wifi密码信息. 步骤: 1.开始----运行----输入cmd 2.在dos窗口输入以下代码: “for /f " ...
- 将win7电脑变身WiFi热点
转自:http://bbs.feng.com/read-htm-tid-2167498.html 开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路 ...
- 电脑忘记WiFi密码了,但又想知道,该怎么办?
如何查看电脑已经连过的WiFi的密码? 你有没有遇到这样的情况,电脑之前连过的WiFi,正好手机也想连此WiFi,但是忘记密码了,没有WiFi的手机怎么能叫手机呢?.下面我们来看看如何查看已连接过的W ...
- ESP8266 HTTP 项目(2)HTTP网页修改WIFI连接,上电自动连接上次的WIFI。
网页 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf ...
- WIN7使用技巧 让电脑变身WiFi热点,让手机、笔记本共享上网,如何开启无线网络,共享无线网络
将win7电脑变身WIFI热点,让手机.笔记本共享上网 用win7建立无线局域网,可以共享上网可以局域网游戏. 开启windows 7的隐藏功能:虚拟WIFI和SoftAP(即虚拟无线AP),就可以让 ...
- 电脑破解wifi密码(至少连过1次的才可以)
电脑破解wifi密码(至少连过1次的才可以) 连过的wifi密码忘记了怎么办? 只要你电脑连过的都能破解. cmd输入以下内容查看电脑连接过的wifi名字. netsh wlan show profi ...
- 电脑变WIFI:建立虚拟共享WIFI热点可查看WIFI密码windows中使用bat批处理命令提示符cmd创建教程含工具
台式机也可支持移动热点,Win10却提示"我们无法设置移动热点",今天我们就另辟蹊径来调教它. 建立和开启虚拟WIFI共享网络 Windows 7操作系统及以后支持承载网络,可 ...
- ubuntu连接不到WiFi
ubuntu连接不到WiFi 在软件与更新中,进入附加驱动. 搜到对应的无线网卡驱动,安装后在重启电脑.
随机推荐
- Javascript基础系列之(七)函数(argument访问函数参数)
argument是javascript中函数的一个特殊参数,例如下文,利用argument访问函数参数,判断函数是否执行 <script type="text/javascript&q ...
- 第三章:Javascript类型、值和变量。
计算机程序的运行需要对值(value)比如数字3.14或者文本"hello world"进行操作,在编程语言中,能够表示并操作的值的类型叫做数据类型(type),编程语言最基本的特 ...
- 改Bug
一:新闻查询失败 1.velocity:R对象里的变量不区分大小写? 哦,应该是的! 2.表单的button是默认就有提交功能的哦! 3.velocity变量在页面上的解析: 为什么会出错呢? 难 ...
- [转]DBA,SYSDBA,SYSOPER三者的区别
原文地址:http://www.oracleonlinux.cn/2010/02/dba_sysdba_sysoper/ 什么是DBA?什么是SYSDBA,什么又是SYSOPER?三者究竟有何联系呢? ...
- FZU2082树链剖分
简单题. #include<queue> #include<stack> #include<cmath> #include<cstdio> #inclu ...
- iOS关于rar解压第三方库Unrar4iOS使用总结
作者最近的公司项目要做实现rar解压的功能,在网上找了很久貌似关于rar解压的资料很少,不过有很多人推荐一个名叫“Unrar4iOS”的第三方开源框架,于是下载并尝试使用发现该开源框架并在使用过程中发 ...
- BIEE连接数据库的方法
BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...
- Linux System Reinforcement、Intrusion Detection Based On syslog
目录 .文件系统及访问权限 . Linux Syslog . Linux日志审计 . 帐号安全管理 . 基础物理安全 . 系统编译环境安全 . 系统病毒.后门.rootkit安全 . 系统端口.服务安 ...
- java将一维数组拆分成二维数组
package staticFactory; public class Array { public static void main(String[] args) { String[] a=new ...
- 最短路之Dijkstra算法
1. 邻接矩阵 int cost[MAX_V][MAX_V]; //assume cost[u][v]>0 int d[MAX_V]; bool used[MAX_V]; void Dijkst ...