C # How to use Wlanapi to connect your PC to WiFi

Source: Internet
Author: User

Http://www.codeproject.com/Articles/72105/Manage-WiFi-with-Native-API-WIFI-on-Windows-XP-SP

Introduction

This article would demonstrate the ability to establish and operate a Wi-Fi 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 is to create a function of returns the WiFi networks available for connection. Using WLANOpenHandle The, you create the client to 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:

  

Step 3

Using the pointer to the list of available interfaces, we'll populate the structure that'll contain all Wlan.WLAN_INTERFACE_INFO_LIST  NIC inter Face information:

  

Step 4

What we is still missing at this point and guidance of our interface to being passed Wlan.WlanGetAvailableNetworkList to the function, we get through T His passage:

Step 5

Now we have the elements to call the API Wlan.WlanGetAvailableNetworkList , declare that we'll 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 = Wlanavai LABLENETWORKLIST.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</k Eytype><protected>false</protected> <keyMaterial>{1}</keyMaterial></sharedKey> <keyIndex>0</keyIndex> </security>&Lt;/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/netwo Rking/wlan/profile/v1\ "> <name>{0}</name><ssidconfig><ssid><name>{0}</name& Gt;</ssid></ssidconfig> <connectiontype>ess</connectiontype><connectionmode>auto& Lt;/connectionmode> <autoswitch>false</autoswitch><msm><security><authencryption&         Gt <authEntication>wpapsk</authentication><encryption>tkip</encryption> <useOneX>false</ Useonex></authencryption><sharedkey> <keyType>passPhrase</keyType><protected> False</protected> &LT;KEYMATERIAL&GT;{1}&LT;/KEYMATERIAL&GT;&LT;/SHAREDKEY&GT;&LT;/SECURITY&GT;&LT;/MSM   ></WLANProfile> ", profilename, key); Wlan.wlansetprofile (Clienthandle, Interfaceguid, Wlan.wlanprofileflag                           S.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;  }

  

Supplementary reference: Http://stackoverflow.com/questions/6318575/connect-to-a-wireless-network-in-c-sharp

C # How to use Wlanapi to connect your PC to WiFi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.