Connect to Wi-Fi C ++

I searched everywhere ... I did! I just could not find any example in how to connect to Wi-Fi in C ++.

I found and tried the WlanGetAvailableNetworkList () and WlanQueryInterface () examples on MSDN. I also found an example of what I'm looking for in C #. Can someone tell me one for C ++ ?

Edit: I don't know anything about the C ++ Internet part (servers, Wifi APIs, not even many Win32 APIs), just the core of the language, I just want to create a simple program that finds an open connection and automatically connects to it and plays a sound if the connection was successful. If you can provide me the info link and I will do research and publish any solution I can find.

+6
source share
2 answers

Well, I think you're looking for an enumeration function like this one:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms706716%28v=vs.85%29.aspx

I assume you mean to check the status of the WLan. If you look carefully, the enumeration function returns a structure that includes isState, which is one of the following:

typedef enum _WLAN_INTERFACE_STATE { wlan_interface_state_not_ready = 0, wlan_interface_state_connected = 1, wlan_interface_state_ad_hoc_network_formed = 2, wlan_interface_state_disconnecting = 3, wlan_interface_state_disconnected = 4, wlan_interface_state_associating = 5, wlan_interface_state_discovering = 6, wlan_interface_state_authenticating = 7 } WLAN_INTERFACE_STATE, *PWLAN_INTERFACE_STATE; 

To actually โ€œconnect,โ€ you need the server to listen on the other side ... Although Renan also gave you a good link (see the comments section), it requires that you have an SSID. It depends on whether your software really knows the Wi-Fi SSID.

+1
source

This code is not promoted, i.e. not so good, but he does the job. I use code in windows.

 #include <iostream> #include<stdlib.h> #include<fstream> #include<string> #include<conio.h> using namespace std; int main() { ofstream xmlFile; ifstream xmlFile1; string name="",pass=""; string ntyp="Wi-Fi",netType,fileTest=">test.txt",check,ntype,fil,xfileName,fileName="myWlan.xml"; char c='"',cho='2',cho1='1',c1; netType=c+ntyp+c+fileTest; xfileName=c+fileName+c; int succ=0; do { system("netsh wlan show networks"); cout<<" >-------------------- TO REFRESS PRESS :1 \n\n >-------------------- TO CHOOSE NETWORK PRESS : 2 \n\n > "; cho=getch(); }while(cho!='2'); cout<<"\n Enter the desired network name-------: "; cin>>name; do { cout<<"\n Enter wifi Password------: "; cin>>pass; xmlFile.open(fileName.c_str()); //Writing a xml file ..................... xmlFile<<"<?xml version="<<c<<"1.0"<<c<<"?>\n"; xmlFile<<"<WLANProfile xmlns="<<c<<"http://www.microsoft.com/networking/WLAN/profile/v1"<<c<<">\n"; xmlFile<<"<name>"; xmlFile<<name; xmlFile<<"</name>\n<SSIDConfig>\n<SSID>\n<hex>"; for(int i=0;i<name.length();i++) xmlFile<<hex<<(int)name.at(i); xmlFile<<"</hex>\n<name>"; xmlFile<<name; xmlFile<<"</name>\n</SSID>\n</SSIDConfig>\n<connectionType>ESS</connectionType>\n<connectionMode>auto</connectionMode>\n<MSM>\n<security>\n<authEncryption>"; xmlFile<<"\n<authentication>WPA2PSK</authentication>\n<encryption>AES</encryption>\n<useOneX>false</useOneX>\n</authEncryption>\n<sharedKey>"; xmlFile<<"\n<keyType>passPhrase</keyType>\n<protected>false</protected>\n<keyMaterial>"; xmlFile<<pass; xmlFile<<"</keyMaterial>\n</sharedKey>\n</security>\n</MSM>\n"; xmlFile<<"<MacRandomization xmlns="<<c<<"http://www.microsoft.com/networking/WLAN/profile/v3"<<c<<">\n"; xmlFile<<"<enableRandomization>false</enableRandomization>\n</MacRandomization>\n</WLANProfile>"; xmlFile.close(); //addd the xml file to system profile............. system(("netsh wlan add profile filename="+xfileName).c_str()); //to let system realize what changes have been made............... system("timeout /t 2"); //to check if connected........... system(("netsh interface show interface name="+netType).c_str()); xmlFile1.open("test.txt"); while(!xmlFile1.eof()) { xmlFile1>>c1; if(c1==':') { for(int i=0;i<9;i++) { xmlFile1>>c1; check=check+c1; } } if(check=="Connected") { cout<<"...............................................You are connected!!................................."; succ=1;break; } if(check!="Connected")check=""; } xmlFile1.close(); if(succ==1)break; }while(succ!=1); return 0; } 

HOPES TO HELP ....

+1
source

Source: https://habr.com/ru/post/952999/


All Articles