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 在软件与更新中,进入附加驱动. 搜到对应的无线网卡驱动,安装后在重启电脑.
随机推荐
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- java连接sql server2000/2005
接触Java或者JSP,难免会使用到数据库SQL Server 2000/2005(我使用2005标准版[9.0.3054]测试),经过自己的搜索和研究,使用JDBC连接SQL Server成功,特此 ...
- 压缩算法实现之LZ78
LZ78编码 LZ78算法,建立词典的算法. LZ78的编码思想: 不断地从字符流中提取新的缀-符串(String),通俗地理解为新"词条",然后用"代号"也就 ...
- poppin_xpower_ 常城
- Java 集合类详解(含类图)
0.参考文献 此图中蓝色为抽象类.深红色表示接口(Arrays除外).绿色表示具体容器类 1.java集合类图 1.1 1.2 上述类图中,实线边框的是实现类,比如ArrayList,LinkedLi ...
- CVE-2014-6321 && MS14-066 Microsoft Schannel Remote Code Execution Vulnerability Analysis
目录 . 漏洞的起因 . 漏洞原理分析 . 漏洞的影响范围 . 漏洞的利用场景 . 漏洞的POC.测试方法 . 漏洞的修复Patch情况 . 如何避免此类漏洞继续出现 1. 漏洞的起因 这次的CVE和 ...
- Windows Management Instrumentation WMI Security Technology Learning
目录 . 引言 . WMI(Windows Management Instrumentation)简介 . 基于WMI的攻击向量 . WMI编程示例 0. 引言 在进行服务器主机的入侵检测.安全攻防的 ...
- Erlang之父的学习历史及学习建议
当我开始学习编程的时候(1967年),我可以在 FORTRAN 和(传说中的)Algol 之间选择,不过没有任何人了解 Algol,所以我选择了 FORTRAN. 在我最早学习编程的时候,我的编程周期 ...
- c++内存分配(new和delete)
c中malloc和free是函数,包含在stdlib.h头文件中,分配成功返回指针,失败返回空指针. 与new的区别是: 1,malloc与free是C++/C语言的标准库函数,new/delete是 ...
- React Native 开发之 (02) 用Sublime 3作为React Native的开发IDE
Sublime Text是一个代码编辑器.也是HTML和散文先进的文本编辑器.漂亮的用户界面和非凡的功能,例如:迷你地图,多选择Python插件,代码段等等.完全可自定义键绑定,菜单和工具栏等等.漂亮 ...