#define _WIN32_DCOM
#include"stdafx.h"
#include <windows.h>
#include <conio.h>
#include <objbase.h>
#include <rpcsal.h>
#include <objbase.h>
#include <msxml6.h>
#include <atlbase.h>
#include <iomanip>
#include <wlanapi.h>
using namespace std;
#define WIN32_FROM_HRESULT(hr) (SUCCEEDED(hr) ? ERROR_SUCCESS :(HRESULT_FACILITY(hr) == FACILITY_WIN32 ? HRESULT_CODE(hr) : (hr)))
GetInterfaceStateString(__in WLAN_INTERFACE_STATE wlanInterfaceState)
{
LPWSTR strRetCode;
switch(wlanInterfaceState)
{
case wlan_interface_state_not_ready:
strRetCode = L"\"not ready\"";
break;
case wlan_interface_state_connected:
strRetCode = L"\"connected\"";
break;
case wlan_interface_state_ad_hoc_network_formed:
strRetCode = L"\"ad hoc network formed\"";
break;
case wlan_interface_state_disconnecting:
strRetCode = L"\"disconnecting\"";
break;
case wlan_interface_state_disconnected:
strRetCode = L"\"disconnected\"";
break;
case wlan_interface_state_associating:
strRetCode = L"\"associating\"";
break;
case wlan_interface_state_discovering:
strRetCode = L"\"discovering\"";
break;
case wlan_interface_state_authenticating:
strRetCode = L"\"authenticating\"";
break;
default:
strRetCode = L"\"invalid interface state\"";
}
return strRetCode;
}
LPWSTRGetAcmNotificationString(__in DWORD acmNotif)
{
LPWSTR strRetCode;
switch(acmNotif)
{
case wlan_notification_acm_autoconf_enabled:
strRetCode = L"\"autoconf enabled\"";
break;
case wlan_notification_acm_autoconf_disabled:
strRetCode = L"\"autoconf disabled\"";
break;
case wlan_notification_acm_background_scan_enabled:
strRetCode = L"\"background scan enabled\"";
break;
case wlan_notification_acm_background_scan_disabled:
strRetCode = L"\"background scan disabled\"";
break;
case wlan_notification_acm_power_setting_change:
strRetCode = L"\"power setting change\"";
break;
case wlan_notification_acm_scan_complete:
strRetCode = L"\"scan complete\"";
break;
case wlan_notification_acm_scan_fail:
strRetCode = L"\"scan fail\"";
break;
case wlan_notification_acm_connection_start:
strRetCode = L"\"connection start\"";
break;
case wlan_notification_acm_connection_complete:
strRetCode = L"\"connection complete\"";
break;
case wlan_notification_acm_connection_attempt_fail:
strRetCode = L"\"connection fail\"";
break;
case wlan_notification_acm_filter_list_change:
strRetCode = L"\"filter list change\"";
break;
case wlan_notification_acm_interface_arrival:
strRetCode = L"\"interface arrival\"";
break;
case wlan_notification_acm_interface_removal:
strRetCode = L"\"interface removal\"";
break;
case wlan_notification_acm_profile_change:
strRetCode = L"\"profile change\"";
break;
case wlan_notification_acm_profiles_exhausted:
strRetCode = L"\"profiles exhausted\"";
break;
case wlan_notification_acm_network_not_available:
strRetCode = L"\"network not available\"";
break;
case wlan_notification_acm_network_available:
strRetCode = L"\"network available\"";
break;
case wlan_notification_acm_disconnecting:
strRetCode = L"\"disconnecting\"";
break;
case wlan_notification_acm_disconnected:
strRetCode = L"\"disconnected\"";
break;
case wlan_notification_acm_adhoc_network_state_change:
strRetCode = L"\"ad hoc network state changes\"";
break;
default:
strRetCode = L"\"unknown ACM notification\"";
}
return strRetCode;
}
LPWSTR GetMsmNotificationString(__in DWORD msmNotif)
{
LPWSTR strRetCode;
switch(msmNotif)
{
case wlan_notification_msm_associating:
strRetCode = L"\"associating\"";
break;
case wlan_notification_msm_associated:
strRetCode = L"\"associated\"";
break;
case wlan_notification_msm_authenticating:
strRetCode = L"\"authenticating\"";
break;
case wlan_notification_msm_connected:
strRetCode = L"\"connected\"";
break;
case wlan_notification_msm_roaming_start:
strRetCode = L"\"roaming start\"";
break;
case wlan_notification_msm_roaming_end:
strRetCode = L"\"roaming end\"";
break;
case wlan_notification_msm_radio_state_change:
strRetCode = L"\"radio state change\"";
break;
case wlan_notification_msm_signal_quality_change:
strRetCode = L"\"signal quality change\"";
break;
case wlan_notification_msm_disassociating:
strRetCode = L"\"disassociating\"";
break;
case wlan_notification_msm_disconnected:
strRetCode = L"\"disconnected\"";
break;
case wlan_notification_msm_peer_join:
strRetCode = L"\"a peer joins the ad hoc network\"";
break;
case wlan_notification_msm_peer_leave:
strRetCode = L"\"a peer leaves the ad hoc network\"";
break;
case wlan_notification_msm_adapter_removal:
strRetCode = L"\"adapter is in a bad state\"";
break;
default:
strRetCode = L"\"unknown MSM notification\"";
}
return strRetCode;
}
LPWSTR
GetConnectionModeString(__in WLAN_CONNECTION_MODE wlanConnMode)
{
LPWSTR strRetCode;
switch(wlanConnMode)
{
case wlan_connection_mode_profile:
strRetCode = L"\"manual connection with a profile\"";
break;
case wlan_connection_mode_temporary_profile:
strRetCode = L"\"manual connection with a temporary profile\"";
break;
case wlan_connection_mode_discovery_secure:
strRetCode = L"\"connection to a secure network without a profile\"";
break;
case wlan_connection_mode_discovery_unsecure:
strRetCode = L"\"connection to an unsecure network without a profile\"";
break;
case wlan_connection_mode_auto:
strRetCode = L"\"automatic connection with a profile\"";
break;
default:
strRetCode = L"\"invalid connection mode\"";
}
return strRetCode;
}
LPWSTR GetPhyTypeString(__in ULONG uDot11PhyType)
{
LPWSTR strRetCode;
switch(uDot11PhyType)
{
case dot11_phy_type_dsss:
strRetCode = L"\"DSSS\"";
break;
case dot11_phy_type_erp:
strRetCode = L"\"802.11g\"";
break;
case dot11_phy_type_fhss:
strRetCode = L"\"FHSS\"";
break;
case dot11_phy_type_hrdsss:
strRetCode = L"\"802.11b\"";
break;
case dot11_phy_type_irbaseband:
strRetCode = L"\"IR-base band\"";
break;
case dot11_phy_type_ofdm:
strRetCode = L"\"802.11a\"";
break;
case dot11_phy_type_any:
strRetCode = L"\"any\"";
break;
default:
strRetCode = L"\"Unknown PHY type\"";
}
return strRetCode;
}
LPWSTR GetBssTypeString(__in DOT11_BSS_TYPE dot11BssType)
{
LPWSTR strRetCode;
switch(dot11BssType)
{
case dot11_BSS_type_infrastructure:
strRetCode = L"\"Infrastructure\"";
break;
case dot11_BSS_type_independent:
strRetCode = L"\"Ad hoc\"";
break;
case dot11_BSS_type_any:
strRetCode = L"\"Any\"";
break;
default:
strRetCode = L"\"Unknown BSS type\"";
}
return strRetCode;
}
LPWSTR GetRadioStateString(__in DOT11_RADIO_STATE radioState)
{
LPWSTR strRetCode;
switch(radioState)
{
case dot11_radio_state_on:
strRetCode = L"\"on\"";
break;
case dot11_radio_state_off:
strRetCode = L"\"off\"";
break;
default:
strRetCode = L"\"unknown state\"";
}
return strRetCode;
}
LPWSTR GetAuthAlgoString(__in DOT11_AUTH_ALGORITHM dot11AuthAlgo)
{
LPWSTR strRetCode = L"\"Unknown algorithm\"";
switch(dot11AuthAlgo)
{
case DOT11_AUTH_ALGO_80211_OPEN:
strRetCode = L"\"Open\"";
break;
case DOT11_AUTH_ALGO_80211_SHARED_KEY:
strRetCode = L"\"Shared\"";
break;
case DOT11_AUTH_ALGO_WPA:
strRetCode = L"\"WPA-Enterprise\"";
break;
case DOT11_AUTH_ALGO_WPA_PSK:
strRetCode = L"\"WPA-Personal\"";
break;
case DOT11_AUTH_ALGO_WPA_NONE:
strRetCode = L"\"WPA-NONE\"";
break;
case DOT11_AUTH_ALGO_RSNA:
strRetCode = L"\"WPA2-Enterprise\"";
break;
case DOT11_AUTH_ALGO_RSNA_PSK:
strRetCode = L"\"WPA2-Personal\"";
break;
default:
if (dot11AuthAlgo & DOT11_AUTH_ALGO_IHV_START)
{
strRetCode = L"\"Vendor-specific algorithm\"";
}
}
return strRetCode;
}
LPWSTR GetCipherAlgoString(__in DOT11_CIPHER_ALGORITHM dot11CipherAlgo)
{
LPWSTR strRetCode = L"\"Unknown algorithm\"";
switch(dot11CipherAlgo)
{
case DOT11_CIPHER_ALGO_NONE:
strRetCode = L"\"None\"";
break;
case DOT11_CIPHER_ALGO_WEP40:
strRetCode = L"\"WEP40\"";
break;
case DOT11_CIPHER_ALGO_TKIP:
strRetCode = L"\"TKIP\"";
break;
case DOT11_CIPHER_ALGO_CCMP:
strRetCode = L"\"AES\"";
break;
case DOT11_CIPHER_ALGO_WEP104:
strRetCode = L"\"WEP104\"";
break;
case DOT11_CIPHER_ALGO_WPA_USE_GROUP:
strRetCode = L"\"USE-GROUP\"";
break;
case DOT11_CIPHER_ALGO_WEP:
strRetCode = L"\"WEP\"";
break;
default:
if (dot11CipherAlgo & DOT11_CIPHER_ALGO_IHV_START)
{
strRetCode = L"\"Vendor-specific algorithm\"";
}
}
return strRetCode;
}
DWORD StringWToSsid(__in LPCWSTR strSsid,__out PDOT11_SSID pSsid)
{
DWORD dwRetCode = ERROR_SUCCESS;
BYTE pbSsid[DOT11_SSID_MAX_LENGTH + 1] = {0};
if (strSsid == NULL || pSsid == NULL)
{
dwRetCode = ERROR_INVALID_PARAMETER;
}
else
{
pSsid->uSSIDLength = WideCharToMultiByte (CP_ACP, 0, strSsid,
-1,
(LPSTR)pbSsid,
sizeof(pbSsid),
NULL,
NULL);
pSsid->uSSIDLength--;
memcpy(&pSsid->ucSSID, pbSsid, pSsid->uSSIDLength);
}
return dwRetCode;
}
LPWSTR SsidToStringW(
__out_ecount(count) LPWSTR buf,
__in ULONG count,
__in PDOT11_SSID pSsid)
{
ULONG bytes, i;
bytes = min( count-1, pSsid->uSSIDLength);
for( i=0; i<bytes;>
mbtowc( &buf[i], (const char *)&pSsid->ucSSID[i], 1);
buf[bytes] = '\0';
return buf;
}
#define WLSAMPLE_REASON_STRING_LEN 256
VOID
PrintReason(
__in WLAN_REASON_CODE reason)
{
WCHAR strReason[WLSAMPLE_REASON_STRING_LEN];
if (WlanReasonCodeToString(reason,WLSAMPLE_REASON_STRING_LEN,strReason,NULL) == ERROR_SUCCESS)
{
wcout << L" The reason is \"" < strReason < L"\"." < endl;
}
else
{
wcout << L" The reason code is " << reason < L"." < endl;
}
}
VOID PrintNetworkInfo(
__in PWLAN_AVAILABLE_NETWORK pNetwork)
{
WCHAR strSsid[DOT11_SSID_MAX_LENGTH+1];
if (pNetwork != NULL)
{
wcout < L"SSID: " < SsidToStringW(strSsid, sizeof(strSsid)/sizeof(WCHAR), &pNetwork->dot11Ssid) < endl;
if (pNetwork->bSecurityEnabled)
{
wcout < L"\tSecurity enabled." < endl;
}
else
{
wcout < L"\tSecurity not enabled." < endl;
}
wcout < L"\tContains " < pNetwork->uNumberOfBssids < L" BSSIDs." < endl;
if (pNetwork->dwFlags & WLAN_AVAILABLE_NETWORK_HAS_PROFILE)
{
wcout < L"\tHas a matching profile: " < pNetwork->strProfileName < L"." <<endl;
}
if (pNetwork->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED)
{
wcout < L"\tCurrently connected." < endl;
}
if (!pNetwork->bNetworkConnectable)
{
wcout < L"\tThe network is not connectable. ";
PrintReason(pNetwork->wlanNotConnectableReason);
}
else
{
wcout < L"\tThe network is connectable." < endl;
}
wcout < L"\tBSS type: " < GetBssTypeString(pNetwork->dot11BssType) < endl;
wcout < L"\tSignal quality: " < pNetwork->wlanSignalQuality < L"%" < endl;
wcout < L"\tDefault authentication algorithm: " < GetAuthAlgoString(pNetwork->dot11DefaultAuthAlgorithm) < endl;
wcout < L"\tDefault cipher algorithm: " < GetCipherAlgoString(pNetwork->dot11DefaultCipherAlgorithm) < endl;
}
}
VOID
PrintBssInfo(__in PWLAN_BSS_ENTRY pBss)
{
WCHAR strSsid[DOT11_SSID_MAX_LENGTH+1];
UINT i;
PBYTE pIe = NULL;
if (pBss != NULL)
{
wcout < L"MAC address: ";
for (i = 0; i 6; i++)
{
wcout < setw(2) < setfill(L'0') < hex < (UINT)pBss->dot11Bssid[i] <<L" ";
}
wcout < endl;
wcout < L"\tSSID: " < SsidToStringW(strSsid, sizeof(strSsid)/sizeof(WCHAR), &pBss->dot11Ssid) < endl;
wcout < L"\tBeacon period: " < dec < pBss->usBeaconPeriod < L" TU" < endl;
wcout << L"\tIE";
i = 0;
pIe = (PBYTE)(pBss) + pBss->ulIeOffset;
while (i < pBss->ulIeSize)
{
if (i % 8 == 0)
{
wcout < endl < L"\t\t";
}
wcout < setw(2) < setfill(L'0') < hex < (UINT)pIe[i] < L" ";
i++;
}
wcout < endl;
}
}
#define WLAN_INVALID_COUNTER (ULONGLONG)-1
VOID
PrintCounterValue(
__in ULONGLONG value)
{
if (value == WLAN_INVALID_COUNTER)
wcout < L" cannot be obtained" < endl;
else
wcout < (UINT)value < endl;
}
VOID
PrintErrorMsg(
__in LPWSTR strCommand,
__in DWORD dwError)
{
if (strCommand != NULL)
{
if (dwError == ERROR_SUCCESS)
{
wcout < L"Command \"" < strCommand < L"\" completed successfully." < endl;
}
else if (dwError == ERROR_INVALID_PARAMETER)
{
wcout < L"The parameter for \"" < strCommand < L"\" is not correct. ";
wcout < L"Please use \"help " < strCommand < L"\" to check the usage of the command." < endl;
}
else if (dwError == ERROR_BAD_PROFILE)
{
wcout < L"The given profile is not valid." < endl;
}
else if (dwError == ERROR_NOT_SUPPORTED)
{
wcout < L"Command \"" < strCommand < L"\" is not supported." < endl;
}
else
{
wcout < L"Got error " < dwError < L" for command \"" < strCommand < L"\"" < endl;
}
}
}
DWORD
OpenHandleAndCheckVersion(
PHANDLE phClient)
{
DWORD dwError = ERROR_SUCCESS;
DWORD dwServiceVersion;
HANDLE hClient = NULL;
__try
{
*phClient = NULL;
if ((dwError = WlanOpenHandle(WLAN_API_VERSION, NULL, &dwServiceVersion, &hClient )) != ERROR_SUCCESS)
{
__leave;
}
if (WLAN_API_VERSION_MAJOR(dwServiceVersion) WLAN_API_VERSION_MAJOR(WLAN_API_VERSION_2_0))
{
}
*phClient = hClient;
hClient = NULL;
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL);
}
}
return dwError;
}
/
Functions that demonstrate how to use WLAN APIs
VOID WINAPI NotificationCallback(
__in PWLAN_NOTIFICATION_DATA pNotifData,
__in_opt PVOID pContext) {
WCHAR strSsid[DOT11_SSID_MAX_LENGTH+1];
PWLAN_CONNECTION_NOTIFICATION_DATA pConnNotifData = NULL;
if (pNotifData != NULL)
{
switch(pNotifData->NotificationSource)
{
case WLAN_NOTIFICATION_SOURCE_ACM:
wcout < L"Got notification " < GetAcmNotificationString(pNotifData->NotificationCode) < L" from ACM." < endl;
switch(pNotifData->NotificationCode)
{
case wlan_notification_acm_connection_complete:
if (pNotifData->dwDataSize sizeof(WLAN_CONNECTION_NOTIFICATION_DATA))
{
break;
}
pConnNotifData = (PWLAN_CONNECTION_NOTIFICATION_DATA)pNotifData->pData;
if (pConnNotifData->wlanReasonCode == WLAN_REASON_CODE_SUCCESS)
{
wcout < L"The connection succeeded." < endl;
if (pConnNotifData->wlanConnectionMode == wlan_connection_mode_discovery_secure ||
pConnNotifData->wlanConnectionMode == wlan_connection_mode_discovery_unsecure)
{
wcout < L"The profile used for this connection is as follows:" < endl;
wcout < pConnNotifData->strProfileXml < endl;
}
}
else
{
wcout < L"The connection failed.";
PrintReason(pConnNotifData->wlanReasonCode);
}
break;
case wlan_notification_acm_connection_start:
if (pNotifData->dwDataSize != sizeof(WLAN_CONNECTION_NOTIFICATION_DATA))
{
break;
}
pConnNotifData = (PWLAN_CONNECTION_NOTIFICATION_DATA)pNotifData->pData;
wcout < L"\tCurrently connecting to " < SsidToStringW(strSsid, sizeof(strSsid)/sizeof(WCHAR), &pConnNotifData->dot11Ssid);
wcout < L" using profile " < pConnNotifData->strProfileName;
wcout < L", connection mode is " < GetConnectionModeString(pConnNotifData->wlanConnectionMode);
wcout < L", BSS type is " < GetBssTypeString(pConnNotifData->dot11BssType) < endl;
break;
}
break;
case WLAN_NOTIFICATION_SOURCE_MSM:
wcout < L"Got notification " < GetMsmNotificationString(pNotifData->NotificationCode) < L" from MSM." < endl;
break;
}
}
} */
VOID RegisterNotification(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
DWORD dwPrevNotifType = 0;
__try
{
if (argc != 1)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanRegisterNotification(
hClient,
WLAN_NOTIFICATION_SOURCE_ACM | WLAN_NOTIFICATION_SOURCE_MSM,
FALSE, NotificationCallback,
NULL, NULL, &dwPrevNotifType
)) != ERROR_SUCCESS)
{
__leave;
}
wcout < L"ACM and MSM notifications are successfully registered. Press any key to exit." < endl;
_getch();
if ((dwError = WlanRegisterNotification(
hClient,
WLAN_NOTIFICATION_SOURCE_NONE,
FALSE, NULL, NULL, NULL, &dwPrevNotifType)) == ERROR_SUCCESS)
{
wcout < L"ACM and MSM notifications are successfully unregistered." < endl;
}
else
{
wcout < L"Error " < dwError < L" occurs when unresiger ACM and MSM notifications." < endl;
}
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID SetProfile(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError;
HRESULT hr;
HANDLE hClient = NULL;
GUID guidIntf;
CComPtr<ixmldomdocument2> pXmlDoc;
CComBSTR bstrXml;
VARIANT_BOOL vbSuccess;
DWORD dwReason;
do
{
if (argc != 3)
{
dwError = ERROR_INVALID_PARAMETER;
break;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
break;
}
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (hr != S_OK)
{
dwError = WIN32_FROM_HRESULT(hr);
break;
}
hr = CoCreateInstance(
CLSID_DOMDocument60,
NULL,
CLSCTX_INPROC_SERVER,
IID_IXMLDOMDocument2,
(void**)&pXmlDoc);
if (hr != S_OK)
{
dwError = WIN32_FROM_HRESULT(hr);
break;
}
hr = pXmlDoc->load((CComVariant)argv[2], &vbSuccess);
if (hr != S_OK || vbSuccess != VARIANT_TRUE)
{
dwError = ERROR_BAD_PROFILE;
break;
}
hr = pXmlDoc->get_xml(&bstrXml);
if (hr != S_OK)
{
dwError = ERROR_BAD_PROFILE;
break;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
break;
}
dwError = WlanSetProfile(
hClient,
&guidIntf,
0, bstrXml,
NULL, TRUE, NULL, &dwReason);
if (dwError == ERROR_BAD_PROFILE)
{
wcout < L"The profile is bad.";
PrintReason(dwReason);
}
} while (FALSE);
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
PrintErrorMsg(argv[0], dwError);
} */
VOID GetProfile(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
PWSTR strXml;
GUID guidIntf;
__try
{
if (argc != 3)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanGetProfile(
hClient,
&guidIntf,
argv[2], NULL, &strXml, NULL, NULL )) == ERROR_SUCCESS)
{
wcout < L"The return profile xml is: " < endl < strXml < endl;
WlanFreeMemory(strXml);
}
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID DeleteProfile(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
__try
{
if (argc != 3)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
dwError = WlanDeleteProfile(
hClient,
&guidIntf,
argv[2], NULL );
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID SetProfileList(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
__try
{
if (argc 3)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
dwError = WlanSetProfileList(
hClient,
&guidIntf,
argc - 2, (LPCWSTR *)(argv + 2), NULL );
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID GetProfileList(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
PWLAN_PROFILE_INFO_LIST pProfileList = NULL;
PWLAN_PROFILE_INFO pInfo = NULL;
UINT i;
__try
{
if (argc != 2)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanGetProfileList(
hClient,
&guidIntf,
NULL, &pProfileList
)) != ERROR_SUCCESS)
{
__leave;
}
wcout < L"There are " < pProfileList->dwNumberOfItems < L" profiles on the interface." < endl;
for (i = 0; i pProfileList->dwNumberOfItems; i++)
{
pInfo = &pProfileList->ProfileInfo[i];
wcout < L"\t\"" < pInfo->strProfileName < L"\"" < endl;
}
}
__finally
{
if (pProfileList != NULL)
{
WlanFreeMemory(pProfileList);
}
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
} */
VOID EnumInterface(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
PWLAN_INTERFACE_INFO_LIST pIntfList = NULL;
RPC_WSTR strGuid = NULL;
UINT i = 0;
__try
{
if (argc != 1)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanEnumInterfaces(
hClient,
NULL, &pIntfList
)) != ERROR_SUCCESS)
{
__leave;
}
wcout < L"There are " < pIntfList->dwNumberOfItems < L" interfaces in the system." < endl;
for (i = 0; i pIntfList->dwNumberOfItems; i++)
{
wcout < L"Interface " < i < L":" < endl;
if (UuidToStringW(&pIntfList->InterfaceInfo[i].InterfaceGuid, &strGuid) == RPC_S_OK)
{
wcout < L"\tGUID: " < (LPWSTR)strGuid < endl;
RpcStringFreeW(&strGuid);
}
wcout < L"\t" < pIntfList->InterfaceInfo[i].strInterfaceDescription < endl;
wcout < L"\tState: " < GetInterfaceStateString(pIntfList->InterfaceInfo[i].isState) < endl;
wcout < endl;
}
}
__finally
{
if (pIntfList != NULL)
{
WlanFreeMemory(pIntfList);
}
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
} */
VOID GetInterfaceCapability(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
PWLAN_INTERFACE_CAPABILITY pCapability = NULL;
PWLAN_AUTH_CIPHER_PAIR_LIST pSupportedAuthCipherList = NULL;
DWORD dwDataSize;
UINT i;
__try
{
if (argc != 2)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if (( dwError = WlanGetInterfaceCapability(
hClient,
&guidIntf,
NULL, &pCapability
)) != ERROR_SUCCESS)
{
__leave;
}
if (pCapability->interfaceType == wlan_interface_type_emulated_802_11)
{
wcout < L"Emulated 802.11 NIC." < endl;
}
else if (pCapability->interfaceType == wlan_interface_type_native_802_11)
{
wcout < L"Native 802.11 NIC." < endl;
}
else
{
wcout < L"Unknown NIC." < endl;
}
wcout < L"Supports " < pCapability->dwNumberOfSupportedPhys < L" PHY types:" < endl;
for (i = 0; i pCapability->dwNumberOfSupportedPhys; i++)
{
wcout < L"\t" < GetPhyTypeString(pCapability->dot11PhyTypes[i]) < endl;
}
if ((dwError = WlanQueryInterface(
hClient,
&guidIntf,
wlan_intf_opcode_supported_infrastructure_auth_cipher_pairs,
NULL, &dwDataSize,
(PVOID *)&(pSupportedAuthCipherList),
NULL )) != ERROR_SUCCESS)
{
__leave;
}
wcout < L"Supported auth cipher pairs (infrastructure):" < endl;
for (i = 0; i pSupportedAuthCipherList->dwNumberOfItems; i++)
{
wcout < L"\t";
wcout < GetAuthAlgoString(pSupportedAuthCipherList->pAuthCipherPairList[i].AuthAlgoId);
wcout < L" and ";
wcout < GetCipherAlgoString(pSupportedAuthCipherList->pAuthCipherPairList[i].CipherAlgoId) < endl;
}
WlanFreeMemory(pSupportedAuthCipherList);
pSupportedAuthCipherList = NULL;
if ((dwError = WlanQueryInterface(
hClient,
&guidIntf,
wlan_intf_opcode_supported_adhoc_auth_cipher_pairs,
NULL, &dwDataSize,
(PVOID *)&(pSupportedAuthCipherList),
NULL )) != ERROR_SUCCESS)
{
__leave;
}
wcout < L"Supported auth cipher pairs (ad hoc):" < endl;
for (i = 0; i pSupportedAuthCipherList->dwNumberOfItems; i++)
{
wcout < L"\t";
wcout < GetAuthAlgoString(pSupportedAuthCipherList->pAuthCipherPairList[i].AuthAlgoId);
wcout < L" and ";
wcout < GetCipherAlgoString(pSupportedAuthCipherList->pAuthCipherPairList[i].CipherAlgoId) < endl;
}
WlanFreeMemory(pSupportedAuthCipherList);
pSupportedAuthCipherList = NULL;
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}*/
VOID SetRadioState(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
PWLAN_INTERFACE_CAPABILITY pInterfaceCapability = NULL;
DWORD i;
WLAN_PHY_RADIO_STATE wlanPhyRadioState;
__try
{
if (argc != 3)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (_wcsicmp(argv[2], L"on") == 0)
{
wlanPhyRadioState.dot11SoftwareRadioState = dot11_radio_state_on;
}
else if (_wcsicmp(argv[2], L"off") == 0)
{
wlanPhyRadioState.dot11SoftwareRadioState = dot11_radio_state_off;
}
else
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanGetInterfaceCapability(
hClient,
&guidIntf,
NULL, &pInterfaceCapability
)) != ERROR_SUCCESS)
{
__leave;
}
for (i = 0; i pInterfaceCapability->dwNumberOfSupportedPhys; i++)
{
wlanPhyRadioState.dwPhyIndex = i;
if ((dwError = WlanSetInterface(
hClient,
&guidIntf,
wlan_intf_opcode_radio_state,
sizeof(wlanPhyRadioState),
(PBYTE)&wlanPhyRadioState,
NULL )) != ERROR_SUCCESS)
{
__leave;
}
}
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
if (pInterfaceCapability != NULL)
{
WlanFreeMemory(pInterfaceCapability);
}
}
PrintErrorMsg(argv[0], dwError);
} */
VOID QueryInterface(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
WLAN_INTERFACE_STATE isState;
PWLAN_CONNECTION_ATTRIBUTES pCurrentNetwork = NULL;
WCHAR strSsid[DOT11_SSID_MAX_LENGTH+1];
WLAN_RADIO_STATE wlanRadioState;
PVOID pData = NULL;
DWORD dwDataSize = 0;
UINT i;
__try
{
if (argc != 2)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanQueryInterface(
hClient,
&guidIntf,
wlan_intf_opcode_radio_state,
NULL, &dwDataSize,
&pData,
NULL )) != ERROR_SUCCESS &&
dwError != ERROR_NOT_SUPPORTED)
{
__leave;
}
if (dwError == ERROR_SUCCESS)
{
if (dwDataSize != sizeof(WLAN_RADIO_STATE))
{
dwError = ERROR_INVALID_DATA;
__leave;
}
wlanRadioState = *((PWLAN_RADIO_STATE)pData);
for (i = 0; i wlanRadioState.dwNumberOfPhys; i++)
{
wcout < L"PHY " < wlanRadioState.PhyRadioState[i].dwPhyIndex < L": " < endl;
wcout < L"\tSoftware radio state is " < GetRadioStateString(wlanRadioState.PhyRadioState[i].dot11SoftwareRadioState) < L"." < endl;
wcout < L"\tHardware radio state is " < GetRadioStateString(wlanRadioState.PhyRadioState[i].dot11HardwareRadioState) < L"." < endl;
}
WlanFreeMemory(pData);
pData = NULL;
}
else
{
wcout < L"Querying radio state is not supported." < endl;
}
if ((dwError = WlanQueryInterface(
hClient,
&guidIntf,
wlan_intf_opcode_interface_state,
NULL, &dwDataSize,
&pData,
NULL )) != ERROR_SUCCESS)
{
__leave;
}
if (dwDataSize != sizeof(WLAN_INTERFACE_STATE))
{
dwError = ERROR_INVALID_DATA;
__leave;
}
isState = *((PWLAN_INTERFACE_STATE)pData);
wcout < L"Interface state: " < GetInterfaceStateString(isState) < L"." < endl;
WlanFreeMemory(pData);
pData = NULL;
if ((dwError = WlanQueryInterface(
hClient,
&guidIntf,
wlan_intf_opcode_current_connection,
NULL, &dwDataSize,
&pData,
NULL )) == ERROR_SUCCESS &&
dwDataSize == sizeof(WLAN_CONNECTION_ATTRIBUTES)
)
{
pCurrentNetwork = (PWLAN_CONNECTION_ATTRIBUTES)pData;
}
if (dwError == ERROR_INVALID_STATE)
{
dwError = ERROR_SUCCESS;
}
if (pCurrentNetwork == NULL)
{
__leave;
}
if (pCurrentNetwork->isState == wlan_interface_state_connected)
wcout < L"Currently connected to ";
else if (pCurrentNetwork->isState == wlan_interface_state_ad_hoc_network_formed)
wcout < L"Currently formed ";
else if (pCurrentNetwork->isState == wlan_interface_state_associating ||
pCurrentNetwork->isState == wlan_interface_state_discovering ||
pCurrentNetwork->isState == wlan_interface_state_authenticating
)
wcout < L"Currently connecting to ";
wcout < SsidToStringW(strSsid, sizeof(strSsid)/sizeof(WCHAR), &pCurrentNetwork->wlanAssociationAttributes.dot11Ssid);
wcout < L" using profile " < pCurrentNetwork->strProfileName;
wcout < L", connection mode is " < GetConnectionModeString(pCurrentNetwork->wlanConnectionMode);
wcout < L", BSS type is " < GetBssTypeString(pCurrentNetwork->wlanAssociationAttributes.dot11BssType) < L"." < endl;
wcout < L"Current PHY type: ";
wcout < GetPhyTypeString(pCurrentNetwork->wlanAssociationAttributes.dot11PhyType) < endl;
}
__finally
{
if (pData != NULL)
{
WlanFreeMemory(pData);
}
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
} */
/scan
VOID Scan(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
__try
{
if (argc != 2)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
dwError = WlanScan(
hClient,
&guidIntf,
NULL, NULL, NULL );
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID GetVisibleNetworkList(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
PWLAN_AVAILABLE_NETWORK_LIST pVList = NULL;
UINT i;
__try
{
if (argc != 2)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanGetAvailableNetworkList(
hClient,
&guidIntf,
0, NULL, &pVList
)) != ERROR_SUCCESS)
{
__leave;
}
wcout < L"Total " < pVList->dwNumberOfItems < L" networks are visible." < endl;
for (i = 0; i pVList->dwNumberOfItems; i++)
{
wcout < L"Network " <<i < L":" < endl;
PrintNetworkInfo(&pVList->Network[i]);
wcout < endl;
}
WlanFreeMemory(pVList);
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID GetDriverStatistics(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
PVOID pData = NULL;
DWORD dwSize;
PWLAN_STATISTICS pStatistics;
UINT i;
__try
{
if (argc != 2)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanQueryInterface(
hClient,
&guidIntf,
wlan_intf_opcode_statistics,
NULL, &dwSize,
&pData,
NULL )) != ERROR_SUCCESS)
{
__leave;
}
pStatistics = (PWLAN_STATISTICS)pData;
wcout < L"Four way handshake failures: \t";
PrintCounterValue(pStatistics->ullFourWayHandshakeFailures);
wcout < L"TKIP Counter Measures invoked: \t";
PrintCounterValue(pStatistics->ullTKIPCounterMeasuresInvoked);
wcout < L"Unicast counters\n";
wcout < L"\tTransmitted frame count: \t";
PrintCounterValue(pStatistics->MacUcastCounters.ullTransmittedFrameCount);
wcout < L"\tReceived frame count: \t";
PrintCounterValue(pStatistics->MacUcastCounters.ullReceivedFrameCount);
wcout < L"\tWEP excluded count: \t";
PrintCounterValue(pStatistics->MacUcastCounters.ullWEPExcludedCount);
wcout < L"Multicast counters\n";
wcout < L"\tTransmitted frame count: \t";
PrintCounterValue(pStatistics->MacMcastCounters.ullTransmittedFrameCount);
wcout < L"\tReceived frame count: \t";
PrintCounterValue(pStatistics->MacMcastCounters.ullReceivedFrameCount);
wcout < L"\tWEP excluded count: \t";
PrintCounterValue(pStatistics->MacMcastCounters.ullWEPExcludedCount);
for (i = 0; i pStatistics->dwNumberOfPhys; i++)
{
wcout < L"PHY " < i < endl;
wcout < L"\tTransmitted frame count: \t";
PrintCounterValue(pStatistics->PhyCounters[i].ullTransmittedFrameCount);
wcout < L"\tMulticast transmitted frame count: \t";
PrintCounterValue(pStatistics->PhyCounters[i].ullMulticastTransmittedFrameCount);
wcout < L"\tReceived frame count: \t";
PrintCounterValue(pStatistics->PhyCounters[i].ullReceivedFrameCount);
wcout < L"\tMulticast received frame count: \t";
PrintCounterValue(pStatistics->PhyCounters[i].ullMulticastReceivedFrameCount);
}
WlanFreeMemory(pData);
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID GetBssList(
__in int argc,
__in_ecount(argc) LPWSTR argv[])
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
DOT11_SSID dot11Ssid = {0};
PDOT11_SSID pDot11Ssid = NULL;
DOT11_BSS_TYPE dot11BssType = dot11_BSS_type_any;
BOOL bSecurityEnabled = TRUE;
PWLAN_BSS_LIST pWlanBssList = NULL;
UINT i;
__try
{
if (argc != 2 && argc != 5)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (argc == 5)
{
if ((dwError = StringWToSsid(argv[2], &dot11Ssid)) != ERROR_SUCCESS)
{
__leave;
}
pDot11Ssid = &dot11Ssid;
if (_wcsicmp(argv[3],L"adhoc") == 0 || _wcsicmp(argv[3], L"a") == 0)
dot11BssType = dot11_BSS_type_independent;
else if (_wcsicmp(argv[3], L"infrastructure") == 0 || _wcsicmp(argv[3], L"i") == 0)
dot11BssType = dot11_BSS_type_infrastructure;
else
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (_wcsicmp(argv[4], L"secure") == 0 || _wcsicmp(argv[4], L"s") == 0)
bSecurityEnabled = TRUE;
else if (_wcsicmp(argv[4], L"unsecure") == 0 || _wcsicmp(argv[4], L"u") == 0)
bSecurityEnabled = FALSE;
else
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
if ((dwError = WlanGetNetworkBssList(
hClient,
&guidIntf,
pDot11Ssid,
dot11BssType,
bSecurityEnabled,
NULL, &pWlanBssList
)) != ERROR_SUCCESS)
{
__leave;
}
for (i = 0; i pWlanBssList->dwNumberOfItems; i++)
{
PrintBssInfo(&pWlanBssList->wlanBssEntries[i]);
}
WlanFreeMemory(pWlanBssList);
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID
Connect(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
)
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
DOT11_SSID dot11Ssid = {0};
WLAN_CONNECTION_PARAMETERS wlanConnPara;
__try
{
if (argc != 5)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = StringWToSsid(argv[2], &dot11Ssid)) != ERROR_SUCCESS)
{
__leave;
}
wlanConnPara.wlanConnectionMode = wlan_connection_mode_profile;
wlanConnPara.strProfile = argv[4];
wlanConnPara.pDot11Ssid = &dot11Ssid;
if (_wcsicmp(argv[3],L"adhoc") == 0 || _wcsicmp(argv[3], L"a") == 0)
wlanConnPara.dot11BssType = dot11_BSS_type_independent;
else if (_wcsicmp(argv[3], L"infrastructure") == 0 || _wcsicmp(argv[3], L"i") == 0)
wlanConnPara.dot11BssType = dot11_BSS_type_infrastructure;
else
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
wlanConnPara.pDesiredBssidList = NULL;
wlanConnPara.dwFlags = 0;
if ((dwError = OpenHandleAndCheckVersion(&hClient)) != ERROR_SUCCESS)
{
__leave;
}
dwError = WlanConnect(hClient, &guidIntf,&wlanConnPara, NULL);
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID
Discover(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
)
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
DOT11_SSID dot11Ssid = {0};
WLAN_CONNECTION_PARAMETERS wlanConnPara;
__try
{
if (argc != 5)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = StringWToSsid(argv[2], &dot11Ssid)) != ERROR_SUCCESS)
{
__leave;
}
wlanConnPara.strProfile = NULL;
wlanConnPara.pDot11Ssid = &dot11Ssid;
if (_wcsicmp(argv[3],L"adhoc") == 0 || _wcsicmp(argv[3], L"a") == 0)
wlanConnPara.dot11BssType = dot11_BSS_type_independent;
else if (_wcsicmp(argv[3], L"infrastructure") == 0 || _wcsicmp(argv[3], L"i") == 0)
wlanConnPara.dot11BssType = dot11_BSS_type_infrastructure;
else
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (_wcsicmp(argv[4], L"secure") == 0 || _wcsicmp(argv[4], L"s") == 0)
wlanConnPara.wlanConnectionMode = wlan_connection_mode_discovery_secure;
else if (_wcsicmp(argv[4], L"unsecure") == 0 || _wcsicmp(argv[4], L"u") == 0)
wlanConnPara.wlanConnectionMode = wlan_connection_mode_discovery_unsecure;
else
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
wlanConnPara.pDesiredBssidList = NULL;
wlanConnPara.dwFlags = 0;
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
dwError = WlanConnect(
hClient,
&guidIntf,
&wlanConnPara,
NULL );
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID
Disconnect(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
)
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
__try
{
if (argc != 2)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
dwError = WlanDisconnect(
hClient,
&guidIntf,
NULL );
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID
SaveTemporaryProfile(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
)
{
DWORD dwError = ERROR_SUCCESS;
HANDLE hClient = NULL;
GUID guidIntf;
DWORD dwFlags = 0;
__try
{
if (argc != 3)
{
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr < L"Invalid GUID " < argv[1] < endl;
dwError = ERROR_INVALID_PARAMETER;
__leave;
}
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
dwError = WlanSaveTemporaryProfile(
hClient,
&guidIntf,
argv[2], NULL, 0, TRUE, NULL );
}
__finally
{
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL );
}
}
PrintErrorMsg(argv[0], dwError);
}
VOID
Help(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
);
typedef VOID (*WLSAMPLE_FUNCTION) (int argc, LPWSTR argv[]);
typedef struct _WLSAMPLE_COMMAND {
LPWSTR strCommandName; LPWSTR strShortHand; WLSAMPLE_FUNCTION Func; LPWSTR strHelpMessage; LPWSTR strParameters; BOOL bRemarks; LPWSTR strRemarks; } WLSAMPLE_COMMAND, *PWLSAMPLE_COMMAND;
WLSAMPLE_COMMAND g_Commands[] = {
{
L"EnumInterface",
L"ei",
EnumInterface,
L"Enumerate wireless interfaces and print the basic interface information.",
L"",
FALSE,
L""
},
{
L"GetInterfaceCapability",
L"gic",
GetInterfaceCapability,
L"Get the capability of an interface.",
L"<interface guid="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"QueryInterface",
L"qi",
QueryInterface,
L"Query the basic information of an interface.",
L"<interface guid="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"SetRadioState",
L"srs",
SetRadioState,
L"Set the software radio state.",
L"<interface guid=""> <on|off>",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"GetDriverStatistics",
L"gds",
GetDriverStatistics,
L"Get driver statistics." ,
L"<interface guid="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"Scan",
L"scan",
Scan,
L"Scan for available wireless networks.",
L"<interface guid="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"GetBssList",
L"gbs",
GetBssList,
L"Get the list of BSS." ,
L"<interface guid=""> [<ssid> <infrastructure(i)|adhoc(a)> <secure(s)|unsecure(u)>]",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"GetVisibleNetworkList",
L"gvl",
GetVisibleNetworkList,
L"Get the list of visible wireless networks.",
L"<interface guid="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"SetProfile",
L"sp",
SetProfile,
L"Save a profile.",
L"<interface guid=""> <profile xml="" file="" name="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"SaveTempProfile",
L"stp",
SaveTemporaryProfile,
L"Save the temporary profile used for the current connection.",
L"<interface guid=""> <profile name="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"GetProfile",
L"gp",
GetProfile,
L"Get the content of a saved profile.",
L"<interface guid=""> <profile name="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"DeleteProfile",
L"dp",
DeleteProfile,
L"Delete a saved profile.",
L"<interface guid=""> <profile name="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"SetProfileList",
L"spl",
SetProfileList,
L"Set the preference order of saved profiles. The list must contain all profiles.",
L"<interface guid=""> <profile name="">+",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"GetProfileList",
L"gpl",
GetProfileList,
L"Get the list of saved profiles, in the preference order." ,
L"<interface guid="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"Connect",
L"conn",
Connect,
L"Connect to a wireless network using a saved profile.",
L"<interface guid=""> <ssid> <infrastructure(i)|adhoc(a)> <profile name="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"Disconnect",
L"dc",
Disconnect,
L"Disconnect from the current network.",
L"<interface guid="">",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"Discover",
L"disc",
Discover,
L"Connect to a network without a saved profile. The WLAN service will discover the settings for connection.",
L"<interface guid=""> <ssid> <infrastructure(i)|adhoc(a)> <secure(s)|unsecure(u)>",
TRUE,
L"Use EnumInterface (ei) command to get the GUID of an interface."
},
{
L"RegisterNotif",
L"r",
RegisterNotification,
L"Register ACM and MSM notifications.",
L"",
FALSE,
L""
},
{
L"help",
L"?",
Help,
L"Print this help message.",
L"[<command>]",
FALSE,
L""
}
};
VOID
Help(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
)
{
UINT i;
if (argc == 1)
{
wcout < L"This is a sample showing how to use WLAN APIs to manager wireless networks." < endl;
wcout < L"The following commands are available. Use \"help xyz\" to show the description of command xyz." < endl;
for (i=0; i sizeof(g_Commands)/sizeof(WLSAMPLE_COMMAND); i++)
{
wcout < L"\t"< g_Commands[i].strCommandName;
wcout < L"(" < g_Commands[i].strShortHand < L")" < endl;
}
}
else if (argc == 2)
{
for (i=0; i sizeof(g_Commands)/sizeof(WLSAMPLE_COMMAND); i++)
{
if (_wcsicmp(argv[1], g_Commands[i].strCommandName) == 0 ||
_wcsicmp(argv[1], g_Commands[i].strShortHand) == 0)
{
wcout < L"Command: " < g_Commands[i].strCommandName;
wcout < L"(" < g_Commands[i].strShortHand < L")" < endl;
wcout < L"Description: " < g_Commands[i].strHelpMessage < endl;
wcout < L"Usage: " < g_Commands[i].strCommandName;
wcout < L"(" < g_Commands[i].strShortHand < L") ";
wcout < g_Commands[i].strParameters < endl;
if (g_Commands[i].bRemarks)
{
wcout < L"Remarks: " < g_Commands[i].strRemarks < endl;
}
break;
}
}
}
else
{
PrintErrorMsg(argv[0], ERROR_INVALID_PARAMETER);
}
}
void
ExecuteCommand(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
)
{
UINT i = 0;
for (i=0; i sizeof(g_Commands)/sizeof(WLSAMPLE_COMMAND); i++)
{
if (_wcsicmp(argv[0], g_Commands[i].strCommandName) == 0 ||
_wcsicmp(argv[0], g_Commands[i].strShortHand) == 0)
{
g_Commands[i].Func(argc, argv);
break;
}
}
if (i == sizeof(g_Commands)/sizeof(WLSAMPLE_COMMAND))
{
wcerr < L"Invalid command " < argv[0] < L"!" < endl;
}
}
int _cdecl
wmain(
__in int argc,
__in_ecount(argc) LPWSTR argv[]
)
{
DWORD dwRetCode = ERROR_SUCCESS;
if (argc = 1)
{
wcout < L"Please type \"" < argv[0] < L" ?\" for help." < endl;
dwRetCode = ERROR_INVALID_PARAMETER;
}
else
{
ExecuteCommand(argc-1, argv+1);
}
return dwRetCode;
}
[edit]Code block added, HTML encoded - OriginalGriff[/edit]