Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#ifndef UNICODE
#define UNICODE
#endif

#pragma comment(lib, "wlanapi.lib")

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <string.h>

#include <wlanapi.h>

int wmain()
{
	DWORD dwResult;
	DWORD con;
	bool status = 0;
	HANDLE hClient;
	DWORD dwRealAPIVer;

	PWLAN_INTERFACE_INFO_LIST pInterfaces;
	PWLAN_INTERFACE_INFO pInterface;

	PWLAN_AVAILABLE_NETWORK_LIST pNetworks;
	PWLAN_AVAILABLE_NETWORK pNetwork;

	// 1) Connecting Wi-Fi
	dwResult = WlanOpenHandle(2, NULL, &dwRealAPIVer, &hClient);
	if (FAILED(dwResult))
	{
		wprintf(L"Can't connect Wi-Fi! Problem function: WlanOpenHandle.");
		_getch();
		return 1;
	}

	// 2) Enumerating Wi-Fi adapters (interfaces)
	dwResult = WlanEnumInterfaces(hClient, NULL, &pInterfaces);
	if (FAILED(dwResult))
	{
		wprintf(L"Can't enumerate WLAN adapters! Problem function: WlanEnumInterfaces.");
		_getch();
		return 2;
	}
	if (pInterfaces->dwNumberOfItems == 0)
	{
		wprintf(L"No Wi-Fi adapter found.");
		_getch();
		return 3;
	}

	// 3) Getting current Wi-Fi adapter.
	pInterface = &pInterfaces->InterfaceInfo[pInterfaces->dwIndex];

	// 4) Getting available networks for this adapter.
	dwResult = WlanGetAvailableNetworkList(hClient, &pInterface->InterfaceGuid, 0, NULL, &pNetworks);
	if (FAILED(dwResult))
	{
		wprintf(L"Can't get available networks! Probably your PC in Airplane mode, and/or Wifi turned out. Problem function: WlanGetAvailableNetworkList.");
		_getch();
		return 4;
	}

	// 5) Enumertating networks
	for (int i = 0; i < (int)pNetworks->dwNumberOfItems; i++)
	{
		// 6) Getting current network from list
		pNetwork = &pNetworks->Network[i];

		// 7) Getting SSID (network's name)
		DOT11_SSID oName = pNetwork->dot11Ssid;
		DOT11_AUTH_ALGORITHM auth = pNetwork->dot11DefaultAuthAlgorithm;
		if (oName.uSSIDLength == 0)
		{
			wprintf(L"(Unknown network name)");
		}
		else
		{
			// 8) Output all characters of SSID
			for (int j = 0; j < (int)oName.uSSIDLength; j++)
			{
				wprintf(L"%c", oName.ucSSID[j]);
			}
		}
		if (auth == 1)
		{
			// parameter for wlanconnect
			WLAN_CONNECTION_PARAMETERS connectionParams;
			connectionParams.wlanConnectionMode = wlan_connection_mode_discovery_unsecure;
			connectionParams.strProfile = NULL;
			char *nameOfSSID = "Window";
			int lengthOfSSID = strlen((const char*)nameOfSSID);
			DOT11_SSID infoForSSID;
			strncpy((char *)(infoForSSID.ucSSID), nameOfSSID, lengthOfSSID);
			infoForSSID.uSSIDLength = lengthOfSSID;
			connectionParams.pDot11Ssid = &infoForSSID;
			connectionParams.pDesiredBssidList = NULL;
			connectionParams.dot11BssType = dot11_BSS_type_independent;
			connectionParams.dwFlags = WLAN_CONNECTION_ADHOC_JOIN_ONLY;

			wprintf(L"\tOpen ");
			// Now connected with open networks
			con = WlanConnect(hClient, &pInterface->InterfaceGuid, &connectionParams, NULL);
			if (FAILED(con))
			{
				wprintf(L"connection not working");
				_getch();
				return 5;
			}
			else
			{
				wprintf(L"connected succesfully");
			}
		}
		else
		{
			wprintf(L"\tSecure");
		}
		wprintf(L"\n");
	}

	_getch();

	// 9) Removing unnecessary lists from RAM
	if (pInterfaces != NULL)
	{
		WlanFreeMemory(pInterfaces);
	}
	if (pNetworks != NULL)
	{
		WlanFreeMemory(pNetworks);
	}

	return 0;
}
Posted
Updated 14-Jun-15 1:23am
v2

While we are doing our best to mind-read who post questions, we still are on the brainstorm phase... So please be more specific!
If your problem is that when WlanConnet() returns the connection is not completed yet please read better about WlanConnect()[^], and you'll discover that the connection is not completed when it returns, but is on the way for completion. To be notified when connection is completed you have to register for a callback notification using WlanRegisterNotification()[^].
The ERROR_SUCCESS value means simply that the WlanConnect() made everything correctly and also correctly started the connection procedure. The outcome of the connection is available through WlanRegisterNotification().
 
Share this answer
 
My problem is that
1. Scan the all wifi.
2. And show Only open wifi.
3. Connect a wifi and disconnected in 5 second

again connected with another wifi and disconnected.

Do Similar connected with all Open wifi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900