Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Wi-Fi C++

Tags:

c++

windows

wifi

I searched everywhere... I did! I just couldn't find any example on how to connect to a Wi-Fi on C++.

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

Edit: I know nothing about the internet part of C++ (servers, Wifi APIs even not much of Win32 APIs), just the core of the language, I just want to make 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 with some info and links I'll do a research and post any solution I can find.

like image 788
Bonnev Avatar asked Sep 02 '13 17:09

Bonnev


2 Answers

Given code is not advance type i.e. not that good but it does the work. I have use codeblock 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;
}

HOPE IT HELPS ....

like image 110
su00n Avatar answered Oct 17 '22 00:10

su00n


Okay, I guess you're looking for an enumeration function such as this one:

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

I guess what you mean is to check whether the WLan state is up. If you look closely the enumeration function returns a structure that includes isState which is one of those:

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 to have a server listening on the other side... Although Renan gave you a good link too (see comment section of question) but that requires you to have an SSID. It depends whether your software would indeed know the Wifi SSIDs.

like image 24
Alexis Wilke Avatar answered Oct 17 '22 01:10

Alexis Wilke