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. [c#基础]DataTable的Select方法

    引言 可以说DataTable存放数据的一个离线数据库,将数据一下加载到内存,而DataReader是在线查询,而且只进形式的查询,如果后退一步,就不可能了,DataTable操作非常方便,但也有缺点 ...

  2. Linq之Linq to XML

    目录 写在前面 系列文章 linq to xml 总结 写在前面 在很多情况下,都可以见到使用xml的影子.例如,在 Web 上,在配置文件.Microsoft Office Word 文件(将wor ...

  3. Ibatis学习总结2--SQL Map XML 配置文件

    SQL Map 使用 XML 配置文件统一配置不同的属性,包括 DataSource 的详细配置信息, SQL Map 和其他可选属性,如线程管理等.以下是 SQL Map 配置文件的一个例子: Sq ...

  4. java操作xm——添加、修改、删除、遍历

    package com.xml.zh; import javax.xml.parsers.*; import javax.xml.transform.Transformer; import javax ...

  5. Linux 进程管理器 supervixor

    使用 supervisor 管理进程 http://www.cnblogs.com/smail-bao/p/5673434.html http://ju.outofmemory.cn/entry/20 ...

  6. yii2系统定义的常用路径别名

    @yii 表示Yii框架所在的目录,也是 yii\BaseYii 类文件所在的位置: @app 表示正在运行的应用的根目录,一般是 digpage.com/frontend :物理路径 @vendor ...

  7. BZOJ-1877 晨跑 最小费用最大流+拆点

    其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...

  8. Erlang练习题----shopping

    直接就上代码了: -module(shop). -export([cost/1,total/1]). cost(orange) -> 5; cost(newspaper) -> 8; co ...

  9. DLUTOJ 1142 高中的公式

    传送门 Time Limit: 1 Sec  Memory Limit: 128 MB Description 据说...高中学习了好多公式.所以...萌学长不知道该用什么公式来解决下面这个问题.对于 ...

  10. Using a Comparison Function for the Key Type

    (这是C++系列随笔的第二篇,这一系列是我练习C++而查的资料) C++ Primer 5th. Ed. pp. 425 ---------------------- Using a Comparis ...