?? wlsample.cpp
字號(hào):
NULL);
pSsid->uSSIDLength--;
memcpy(&pSsid->ucSSID, pbSsid, pSsid->uSSIDLength);
}
return dwRetCode;
}
// copy SSID to a null-terminated WCHAR string
// count is the number of WCHAR in the buffer.
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; i++)
mbtowc( &buf[i], (const char *)&pSsid->ucSSID[i], 1);
buf[bytes] = '\0';
return buf;
}
// the max lenght of the reason string in characters
#define WLSAMPLE_REASON_STRING_LEN 256
// print the reason string
VOID
PrintReason(
__in WLAN_REASON_CODE reason
)
{
WCHAR strReason[WLSAMPLE_REASON_STRING_LEN];
if (WlanReasonCodeToString(
reason,
WLSAMPLE_REASON_STRING_LEN,
strReason,
NULL // reserved
) == ERROR_SUCCESS)
{
wcout << L" The reason is \"" << strReason << L"\"." << endl;
}
else
{
wcout << L" The reason code is " << reason << L"." << endl;
}
}
// print the basic information of a visible wireless network
VOID PrintNetworkInfo(
__in PWLAN_AVAILABLE_NETWORK pNetwork
)
{
WCHAR strSsid[DOT11_SSID_MAX_LENGTH+1];
if (pNetwork != NULL)
{
// SSID
wcout << L"SSID: " << SsidToStringW(strSsid, sizeof(strSsid)/sizeof(WCHAR), &pNetwork->dot11Ssid) << endl;
// whether security is enabled
if (pNetwork->bSecurityEnabled)
{
wcout << L"\tSecurity enabled." << endl;
}
else
{
wcout << L"\tSecurity not enabled." << endl;
}
// number of BSSIDs
wcout << L"\tContains " << pNetwork->uNumberOfBssids << L" BSSIDs." << endl;
// whether have a profile for this SSID
if (pNetwork->dwFlags & WLAN_AVAILABLE_NETWORK_HAS_PROFILE)
{
wcout << L"\tHas a matching profile: " << pNetwork->strProfileName << L"." <<endl;
}
// whether it is connected
if (pNetwork->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED)
{
wcout << L"\tCurrently connected." << endl;
}
// whether it is connectable
if (!pNetwork->bNetworkConnectable)
{
// the reason that it is not connectable
wcout << L"\tThe network is not connectable. ";
PrintReason(pNetwork->wlanNotConnectableReason);
}
else
{
wcout << L"\tThe network is connectable." << endl;
}
// BSS type
wcout << L"\tBSS type: " << GetBssTypeString(pNetwork->dot11BssType) << endl;
// Signal quality
wcout << L"\tSignal quality: " << pNetwork->wlanSignalQuality << L"%" << endl;
// Default auth algorithm
wcout << L"\tDefault authentication algorithm: " << GetAuthAlgoString(pNetwork->dot11DefaultAuthAlgorithm) << endl;
// Default cipher algorithm
wcout << L"\tDefault cipher algorithm: " << GetCipherAlgoString(pNetwork->dot11DefaultCipherAlgorithm) << endl;
}
}
// print BSS info
VOID
PrintBssInfo(
__in PWLAN_BSS_ENTRY pBss
)
{
WCHAR strSsid[DOT11_SSID_MAX_LENGTH+1];
UINT i;
PBYTE pIe = NULL;
if (pBss != NULL)
{
// MAC address
wcout << L"MAC address: ";
for (i = 0; i < 6; i++)
{
wcout << setw(2) << setfill(L'0') << hex << (UINT)pBss->dot11Bssid[i] <<L" ";
}
wcout << endl;
// SSID
wcout << L"\tSSID: " << SsidToStringW(strSsid, sizeof(strSsid)/sizeof(WCHAR), &pBss->dot11Ssid) << endl;
// Beacon period
wcout << L"\tBeacon period: " << dec << pBss->usBeaconPeriod << L" TU" << endl;
// IE
wcout << L"\tIE";
i = 0;
pIe = (PBYTE)(pBss) + pBss->ulIeOffset;
// print 8 byte per line
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
// print the counter value in driver statistics
VOID
PrintCounterValue(
__in ULONGLONG value
)
{
if (value == WLAN_INVALID_COUNTER)
wcout << L" cannot be obtained" << endl;
else
// wcout cannot handle ULONGLONG
wcout << (UINT)value << endl;
}
// print the error message
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;
}
}
}
// open a WLAN client handle and check version
DWORD
OpenHandleAndCheckVersion(
PHANDLE phClient
)
{
DWORD dwError = ERROR_SUCCESS;
DWORD dwServiceVersion;
HANDLE hClient = NULL;
__try
{
*phClient = NULL;
// open a handle to the service
if ((dwError = WlanOpenHandle(
WLAN_API_VERSION,
NULL, // reserved
&dwServiceVersion,
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
// check service version
if (WLAN_API_VERSION_MAJOR(dwServiceVersion) < WLAN_API_VERSION_MAJOR(WLAN_API_VERSION_2_0))
{
// No-op, because the version check is for demonstration purpose only.
// You can add your own logic here.
}
*phClient = hClient;
// set hClient to NULL so it will not be closed
hClient = NULL;
}
__finally
{
if (hClient != NULL)
{
// clean up
WlanCloseHandle(
hClient,
NULL // reserved
);
}
}
return dwError;
}
//
// Functions that demonstrate how to use WLAN APIs
//
// Notification callback function
VOID WINAPI
NotificationCallback(
__in PWLAN_NOTIFICATION_DATA pNotifData,
__in_opt PVOID pContext // this parameter is not used
)
{
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;
// print some notifications as examples
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)
{
// the temporary profile generated for discovery
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;
// print out some connection information
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;
}
}
}
// Register for notification
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;
}
// open a handle to the service
if ((dwError = OpenHandleAndCheckVersion(
&hClient
)) != ERROR_SUCCESS)
{
__leave;
}
// register for ACM and MSM notifications
if ((dwError = WlanRegisterNotification(
hClient,
WLAN_NOTIFICATION_SOURCE_ACM | WLAN_NOTIFICATION_SOURCE_MSM,
FALSE, // do not ignore duplications
NotificationCallback,
NULL, // no callback context is needed
NULL, // reserved
&dwPrevNotifType
)) != ERROR_SUCCESS)
{
__leave;
}
wcout << L"ACM and MSM notifications are successfully registered. Press any key to exit." << endl;
// wait for the user to press a key
_getch();
// unregister notifications
if ((dwError = WlanRegisterNotification(
hClient,
WLAN_NOTIFICATION_SOURCE_NONE,
FALSE, // do not ignore duplications
NULL, // no callback function is needed
NULL, // no callback context is needed
NULL, // reserved
&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
{
// clean up
if (hClient != NULL)
{
WlanCloseHandle(
hClient,
NULL // reserved
);
}
}
PrintErrorMsg(argv[0], dwError);
}
// set profile
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;
// __try and __leave cannot be used here because of COM object
do
{
if (argc != 3)
{
dwError = ERROR_INVALID_PARAMETER;
break;
}
// get the interface GUID
if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
{
wcerr << L"Invalid GUID " << argv[1] << endl;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -