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. 第五章:Javascript语句

    在javascript中,表达式是短语,那么语句(statement)就是整句或命令.正如英文语句以句号结尾,javascript以分号结尾. 表达式计算出一个值,但语句使某件事发生. “使某件事发生 ...

  2. python 参数的组合

    现在我们知道python定义函数的参数类型有:必选参数 默认参数 可变参数 关键字参数 但是在我们日常中我们是可以组合使用这些参数的:但是使用的时候,参数定义是有顺序的 定义的顺序必须是:必选参数,默 ...

  3. 从topcoder赚钱的方法

    1. 算法1.1  SRM 钱少($30左右),而且很难.1.2  Tournament 钱多($1000~$10000),太难~ 2. 设计和开发2.1  构件设计和开发 钱比较多($1000左右) ...

  4. iOS开发小技巧--边接受数据边写入文件的两种方法

    一.NSFileHanle 使用注意点:在往文件写入数据时,必须创建一个空的文件 指定文件写入的方式 -- 覆盖还是追加 最后记得关闭 <1>代码是在大文件传输的练习中截取的.写入数据之前 ...

  5. Cas_Server端安装

        一.Cas Server版本:3.5.2 下载地址:http://download.csdn.net/detail/xiaohuzi1987/5262980   二.安装步骤: 1.解压cas ...

  6. CSS和字符串实现三角形

    听说是百度校招的题目,就写了一下 <!doctype html> <html> <head> <meta charset="utf-8"& ...

  7. 【CodeForces 504A】Misha and Forest

    题 题意 有n个点,代号分别为0到n-1,然后这n个点有d个相连点,相连点的XOR sum 为s,求所有的边. 分析 知识:a^b^a=b,a^b^b=a. 把相连点为1的i存进队列,i的唯一相连点就 ...

  8. hive 使用笔记(partition; HDFS乱码)

    6.  insert 语句 1) 因为目标表有partition, 所以刚开始我使用的语句是 insert overwrite table sa_r_item_sales_day_week_month ...

  9. SpringAOP

    首先导包, 我用的是Spring4.0.4;需要这三个包 Spring-AOP-4.0.4.REALEASE.jar + Spring-aspect-4.0.4.REALEASE.jar +aspec ...

  10. CodeMap

    CodeMap 这是在博客园看到的一位朋友文章介绍的,很好用的插件,所有的方法,注释块在右边一目了然,找代码方便极了,还能设置代码段的高亮,给代码段设置标识