Hey i started working on a windows c/c++ project and the first thing i wanted to do is try to learn as much as i can on the wifi network around me.I started by getting to know my wifi adapter all the way to the available networks.I quickly browsed msdn set of wifi functions and this is what i have so far:
#ifndef UNICODE
#define UNICODE
#endif
//Define Unicode constant
#include <windows.h>
#include <wlanapi.h> //Header for interfacing with wifi functions
#include <objbase.h> //Need Function to help in converting GUID to a readable format
#include <wtypes.h> //helps to deal with wide char
#include <stdio.h>
#include <stdlib.h>
// Need to link with Wlanapi.lib and Ole32.lib
#pragma comment(lib, "wlanapi.lib")
#pragma comment(lib, "ole32.lib")
int wmain()
{
// Declare and initialize variables.
HANDLE hClient = NULL;
DWORD dwMaxClient = 2; //
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
int iRet = 0;
WCHAR GuidString[40] = {0};
int i;
const GUID*ptr;
/* variables used for WlanEnumInterfaces */
PWLAN_INTERFACE_INFO_LIST EnumList = NULL;
PWLAN_INTERFACE_INFO EnumInfo = NULL;
/*variables used for geting interface capability*/
PWLAN_INTERFACE_CAPABILITY pGetCap=NULL;
/*variables used to get SSID and networks available*/
PWLAN_AVAILABLE_NETWORK_LIST netwklist=NULL;
PWLAN_AVAILABLE_NETWORK Netlist=NULL;
PDOT11_SSID ssid=NULL;
//Get Handle to Wlan
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
// FormatMessage can be used to find out why the function failed
return 1;
}
dwResult = WlanEnumInterfaces(hClient, NULL, &EnumList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
return 1;
}
else {
wprintf(L"Enumerated Devices in the Computer:\n");
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"Num Entries: %lu\n", EnumList->dwNumberOfItems);
wprintf(L"Current Index: %lu\n", EnumList->dwIndex);
for (i = 0; i < (int) EnumList->dwNumberOfItems; i++) {
EnumInfo = (WLAN_INTERFACE_INFO *) &EnumList->InterfaceInfo;
wprintf(L"Interface Index[%d]: %lu\n", i, i);
iRet = StringFromGUID2(EnumInfo->InterfaceGuid, (LPOLESTR) &GuidString, 39);
if (iRet == 0)
wprintf(L"StringFromGUID2 failed\n");
else {
wprintf(L"InterfaceGUID[%d]: %ws\n",i, GuidString);
}
wprintf(L"Interface Description[%d]: %ws", i,
EnumInfo->strInterfaceDescription);
wprintf(L"\n");
wprintf(L"Interface State[%d]:", i);
switch (EnumInfo->isState) {
case wlan_interface_state_not_ready:
wprintf(L"Not ready\n");
break;
case wlan_interface_state_connected:
wprintf(L"Connected\n");
break;
case wlan_interface_state_ad_hoc_network_formed:
wprintf(L"First node in a ad hoc network\n");
break;
case wlan_interface_state_disconnecting:
wprintf(L"Disconnecting\n");
break;
case wlan_interface_state_disconnected:
wprintf(L"Not connected\n");
break;
case wlan_interface_state_associating:
wprintf(L"Attempting to associate with a network\n");
break;
case wlan_interface_state_discovering:
wprintf(L"Auto configuration is discovering settings for the network\n");
break;
case wlan_interface_state_authenticating:
wprintf(L"In process of authenticating\n");
break;
default:
wprintf(L"Unknown state %ld\n", EnumInfo->isState);
break;
}
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"Getting Interface Capabilities info\n");
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"--------------------------------------------------------------\n");
ptr=&EnumInfo->InterfaceGuid;
dwResult=WlanGetInterfaceCapability(hClient,ptr,NULL,&pGetCap);
if(dwResult!=ERROR_SUCCESS)
{
wprintf(L"Error %d while getting Interface capability!\n",dwResult);
}
else{
wprintf(L"InterFace Type:%d\n",pGetCap->interfaceType);
wprintf(L"Support for 802.11d:%s\n",pGetCap->bDot11DSupported);
wprintf(L"Maximum size of the SSID list:%d\n",pGetCap->dwMaxDesiredSsidListSize);
wprintf(L"maximum size of the basic service set:%d\n",pGetCap->dwMaxDesiredBssidListSize);
wprintf(L"number of supported PHY types:%d\n",pGetCap->dwNumberOfSupportedPhys);
}
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"Wifi Scan information\n");
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"--------------------------------------------------------------\n");
dwResult=WlanScan(hClient,ptr,ssid,0,0);
if(dwResult!=ERROR_SUCCESS)
wprintf(L"Error will Scanning for networks!\n");
else{
dwResult=WlanGetAvailableNetworkList(hClient,ptr,0x00000002,NULL,&netwklist);
if(dwResult!=ERROR_SUCCESS)
wprintf(L"Error get network list check param.\n");
else{
if(netwklist->dwNumberOfItems>0)
{
wprintf(L"Number of a vailable wifi networks:%d\n",netwklist->dwNumberOfItems);
for(unsigned int i=0;i<netwklist->dwNumberOfItems;i++)
{
Netlist=(WLAN_AVAILABLE_NETWORK *)&netwklist->Network;
wprintf(L"Network Details %d:\n",i);
wprintf(L"--------------------------------------------------------------\n");
wprintf(L"Network Name:%s\n",Netlist->strProfileName);
wprintf(L"Number of BSSIDs in the network:%d\n",Netlist->uNumberOfBssids);
//Not that you can create a switch,fuction detail the reason why WLAN is not connectable.
//check WLAN_REASON_CODE.
if(Netlist->bNetworkConnectable==1 && Netlist->bSecurityEnabled==1)
wprintf(L"Connected to Network and Security is enabled.\n");
else if(Netlist->bNetworkConnectable ==1 && Netlist->bSecurityEnabled!=1)
wprintf(L"Connected to network and Security disabled.\n");
else
wprintf(L"Not connected!\n");
wprintf(L"Signal Quality:%d\n",Netlist->wlanSignalQuality);
wprintf(L"Security Algorithm:%d\tneed more formating\n",Netlist->dot11DefaultAuthAlgorithm);
wprintf(L"Cyphier Algorithm:%d\tneed more formating\n",Netlist->dot11DefaultCipherAlgorithm);
wprintf(L"--------------------------------------------------------------\n");
}
}
else
wprintf(L"No Wifi Networks Available\n");
}
}
wprintf(L"\n");
wprintf(L"-------------------------------------------------------\n");
wprintf(L"-------------------------------------------------------\n");
wprintf(L"End!\n");
}
}
if (EnumList != NULL) {
WlanFreeMemory(EnumList);
EnumList = NULL;
}
getchar();
return 0;
}
try it out!