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的更多相关文章

  1. 使用cmd查看电脑连接过的wifi密码并将密码发送至指定邮箱(三)

    之前,我写了使用cmd查看电脑连接过的wifi密码(二)和使用cmd查看电脑连接过的wifi密码(一)但其中的功能不完善,在本次的更新中新增了更多的功能,其实2018/10/24 就更新完成了,一直拖 ...

  2. 查看window系统电脑连接过的wifi密码

    电脑连接过的wifi都会有痕迹,包括SSID号和密码等信息,借此可以回查wifi密码信息. 步骤: 1.开始----运行----输入cmd 2.在dos窗口输入以下代码: “for /f " ...

  3. 将win7电脑变身WiFi热点

    转自:http://bbs.feng.com/read-htm-tid-2167498.html 开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路 ...

  4. 电脑忘记WiFi密码了,但又想知道,该怎么办?

    如何查看电脑已经连过的WiFi的密码? 你有没有遇到这样的情况,电脑之前连过的WiFi,正好手机也想连此WiFi,但是忘记密码了,没有WiFi的手机怎么能叫手机呢?.下面我们来看看如何查看已连接过的W ...

  5. ESP8266 HTTP 项目(2)HTTP网页修改WIFI连接,上电自动连接上次的WIFI。

    网页 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf ...

  6. WIN7使用技巧 让电脑变身WiFi热点,让手机、笔记本共享上网,如何开启无线网络,共享无线网络

    将win7电脑变身WIFI热点,让手机.笔记本共享上网 用win7建立无线局域网,可以共享上网可以局域网游戏. 开启windows 7的隐藏功能:虚拟WIFI和SoftAP(即虚拟无线AP),就可以让 ...

  7. 电脑破解wifi密码(至少连过1次的才可以)

    电脑破解wifi密码(至少连过1次的才可以) 连过的wifi密码忘记了怎么办? 只要你电脑连过的都能破解. cmd输入以下内容查看电脑连接过的wifi名字. netsh wlan show profi ...

  8. 电脑变WIFI:建立虚拟共享WIFI热点可查看WIFI密码windows中使用bat批处理命令提示符cmd创建教程含工具

    台式机也可支持移动热点,Win10却提示"我们无法设置移动热点",今天我们就另辟蹊径来调教它. ​​建立和开启虚拟WIFI共享网络 Windows 7操作系统及以后支持承载网络,可 ...

  9. ubuntu连接不到WiFi

    ubuntu连接不到WiFi 在软件与更新中,进入附加驱动. 搜到对应的无线网卡驱动,安装后在重启电脑.

随机推荐

  1. Future模式

    Future模式简介 Future模式有点类似于网上购物,在你购买商品,订单生效之后,你可以去做自己的事情,等待商家通过快递给你送货上门.Future模式就是,当某一程序提交请求,期望得到一个答复.但 ...

  2. RAID 磁盘状态为foreign,怎么变成ready

    我们在关掉电源,插入三块硬盘,打开电源 这时候: 首先启动服务器,出现[Press<ctrl><R>to Run Configuration Utility]提示时,按下ctr ...

  3. TRUNC函数,ORA-01898 精度说明符过多

    TRUNC(SYSDATE)即可默认当前日期(年月日),TRUNC(SYSDATE,'yyyy-mm-dd'),精度说明符过多

  4. Spring-事物传播行为

    spring事物的传播属性(7种) REQUIRED(默认) 业务方法需要在一个容器里运行.如果方法运行时,已经处在一个事务中,那么加入到这个事务,否则自己新建一个新的事务. 存在事物,则使用当前事物 ...

  5. 【Matplotlib】 标注细节注意

    相关文档: Artists BBox 由于蓝线和红线的存在,现在刻度标注很难看清楚.我们可以使他们更大,也可以使它们的属性以便使得线呈现半透明的白色背景.这样做我们既可以看到数据也可以看到刻度标注了. ...

  6. 【bzoj1853】 Scoi2010—幸运数字

    http://www.lydsy.com/JudgeOnline/problem.php?id=1853 (题目链接) 今天考试考了容斥,结果空知道结论却不会写→_→ 题意 求区间中不含6,8两个数字 ...

  7. SQLServer 开启远程访问,也可逆向思维进行关闭

    为了可以通过TCP/IP协议远程访问SQLServer数据库,需要做以下几点: 在SQLServer所运行的服务器上,我们必须找到SQLServer所侦听的端口然后添加到WIndows防火墙的[允许入 ...

  8. Maven学习笔记-01-Maven入门

    一 Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1  项 ...

  9. event.keycode值大全

    window.event.keycode-获取按下的键盘值   event.keycode值大全1 keycode 8 = BackSpace BackSpace 2 keycode 9 = Tab ...

  10. Initialization of deep networks

    Initialization of deep networks 24 Feb 2015Gustav Larsson As we all know, the solution to a non-conv ...