#include <windows.h>
#include <wlanapi.h>
#include <wtypes.h>
#include <stdio.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 = nullptr;
DWORD dwMaxClient = 2;
DWORD dwCurVersion = 0;
DWORD dwResult;
/* variables used for WlanEnumInterfaces */
PWLAN_INTERFACE_INFO_LIST pIfList = nullptr;
// test
PWLAN_BSS_LIST ppWlanBssList = nullptr;
WLAN_BSS_ENTRY wlanBssEntry;
// test
dwResult = WlanOpenHandle(dwMaxClient, nullptr, &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, nullptr, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
// FormatMessage can be used to find out why the function failed
return 1;
}
dwResult = WlanScan(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid, nullptr, nullptr, nullptr);
if (dwResult != ERROR_SUCCESS)
{
wprintf(L"WlanScan failed with error: %u\n", dwResult);
return 1;
}
Sleep(3000);
dwResult = WlanGetNetworkBssList(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid, nullptr, dot11_BSS_type_any, true, nullptr, &ppWlanBssList);
if (dwResult != ERROR_SUCCESS)
{
wprintf(L"WlanGetNetworkBssList failed with error: %u\n", dwResult);
return 1;
}
for (DWORD i = 0; i < ppWlanBssList->dwNumberOfItems; i++)
{
printf("%s\r\n", ppWlanBssList->wlanBssEntries[i].dot11Ssid.ucSSID);
if (ppWlanBssList->wlanBssEntries[i].dot11Ssid.ucSSID[0] == 'R' &&
ppWlanBssList->wlanBssEntries[i].dot11Ssid.ucSSID[1] == 'T' &&
ppWlanBssList->wlanBssEntries[i].dot11Ssid.ucSSID[2] == 'P')
{
wlanBssEntry = ppWlanBssList->wlanBssEntries[i];
}
}
WLAN_CONNECTION_PARAMETERS wLanConParam;
wLanConParam.strProfile = L"<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>NAME</name><SSIDConfig><SSID><name>NAME</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>WPA2PSK</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>PASSWORD</keyMaterial></sharedKey></security></MSM></WLANProfile>";
wLanConParam.dwFlags = WLAN_CONNECTION_HIDDEN_NETWORK;
wLanConParam.wlanConnectionMode = wlan_connection_mode_temporary_profile;
wLanConParam.pDot11Ssid = &wlanBssEntry.dot11Ssid;
wLanConParam.pDesiredBssidList = nullptr;
wLanConParam.dot11BssType = wlanBssEntry.dot11BssType;
dwResult = WlanConnect(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid, &wLanConParam, nullptr);
Sleep(5000);
if (dwResult != ERROR_SUCCESS)
{
wprintf(L"WlanConnect failed with error: %u\n", dwResult);
return 1;
}
if (pIfList != nullptr) {
WlanFreeMemory(pIfList);
pIfList = nullptr;
}
return 0;
}
|