
In the process of working on one of his programs, he faced the need to obtain a list of available wireless networks. In the process of searching for information, it turned out that, starting with Windows XP SP2, the Wifi API appeared, which can help solve this problem.
In this article I want to talk about how to write a simple program that displays a list of available wireless networks.
This example will work starting with Windows XP SP2, only for SP2 you will have to download and install the
Wireless LAN API package . If you have Windows XP SP3 and above (Windows Vista, Windows 7), then you don’t need to download anything, you already have everything to work on your machine.
First you need to include the Wlanapi header file in the project, and add the library
#include <Wlanapi.h>
#pragma comment(lib, "Wlanapi.lib" )
It all starts with creating a client session by calling
WlanOpenHandle')
DWORD WINAPI WlanOpenHandle(
__in DWORD dwClientVersion,
__reserved PVOID pReserved,
__out PDWORD pdwNegotiatedVersion,
__out PHANDLE phClientHandle
);
You can either check the version of the operating system and transfer as version (
dwClientVersion ) 1 for Windows XP, 2 for Windows Vista and higher, or always transfer 2. Even in Windows XP, the function call will be successful, and
pdwNegotiatedVersion will point us to the OS selected by .
The client has been created, the handle to it has been received, now it is necessary to choose the interface with which we will work. The list of available interfaces we get by calling
WlanEnumInterfacesDWORD WINAPI WlanEnumInterfaces(
__in HANDLE hClientHandle,
__reserved PVOID pReserved,
__out PWLAN_INTERFACE_INFO_LIST *ppInterfaceList
);
Here
hClientHandle is the handle we received as a result of the WlanOpenHandle call, and
ppInterfaceList is a pointer to the list of available interfaces.
A member of the
WLAN_INTERFACE_INFO_LIST structure -
dwNumberOfItems will tell us about the number of available interfaces.
Now we can call
WlanGetAvailableNetworkList and get a list of available networks.
DWORD WINAPI WlanGetAvailableNetworkList(
__in HANDLE hClientHandle,
__in const GUID *pInterfaceGuid,
__in DWORD dwFlags,
__reserved PVOID pReserved,
__out PWLAN_AVAILABLE_NETWORK_LIST *ppAvailableNetworkList
);
Here
pInterfaceGuid is a pointer to the GUID of the interface, we can get it from the
WLAN_INTERFACE_INFO structure, the list of which we already have after calling WlanEnumInterfaces.
The
dwFlags parameter
has the following values:
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES and
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES .
The ultimate goal of
ppAvailableNetworkList is a pointer to the
WLAN_AVAILABLE_NETWORK_LIST structure, which in turn contains an array of
WLAN_AVAILABLE_NETWORK structures, which in turn describe the parameters of each network found.
After we received what we wanted, it is necessary to free all pointers and close all handles. Pointers to the list of interfaces and the list of networks are freed by calling
WlanFreeMemoryVOID WINAPI WlanFreeMemory(
__in PVOID pMemory
);
We close client session using
WlanCloseHandleDWORD WINAPI WlanCloseHandle(
__in HANDLE hClientHandle,
__reserved PVOID pReserved
);
As you can see from the article - nothing complicated, to make it even easier, I wrote a wrapper class that has the following methods:
//
BOOL OpenClient();
//
BOOL CloseClient();
// ?
BOOL IsClientOpen();
//
BOOL GetAvialableNetworks(GUID interfaceGuid, vector<WLAN_AVAILABLE_NETWORK> &networks);
//
BOOL GetInterfacesList(vector<WLAN_INTERFACE_INFO> &interfaces);
Example of use:
CWiFiManager wifiManager;
if (wifiManager.OpenClient())
{
vector<WLAN_INTERFACE_INFO> interfaces;
vector<WLAN_AVAILABLE_NETWORK> networks;
if (wifiManager.GetInterfacesList(interfaces))
{
if (wifiManager.GetAvialableNetworks(interfaces[0].InterfaceGuid, networks))
{
for (size_t i = 0; i < networks.size(); ++i)
{
// -
}
}
}
}
You can download an example from source
here.