⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wlsample.cpp

📁 VC++ WIFI
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*++

Copyright (c) 2006 Microsoft Corporation

Module Name:

    wlsample.cpp

Abstract:

    Sample code for WLAN APIs

Date:
	11/08/2005  created
    08/22/2006  modified

Environment:

   User mode only

--*/
// define this flag for COM
#define _WIN32_DCOM

#include <windows.h>
#include <conio.h>
#include <objbase.h>
#include <rpcsal.h>
#include <objbase.h>
#include <msxml6.h>
#include <atlbase.h>
#include <iostream>
#include <iomanip>
// headers needed to use WLAN APIs 
#include <wlanapi.h>

using namespace std;

// get win32 error from HRESULT
#define WIN32_FROM_HRESULT(hr)           \
    (SUCCEEDED(hr) ? ERROR_SUCCESS :    \
        (HRESULT_FACILITY(hr) == FACILITY_WIN32 ? HRESULT_CODE(hr) : (hr)))


//
// Utility functions
//

// get interface state string
LPWSTR
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;
}

// get ACM notification string
LPWSTR
GetAcmNotificationString(
    __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;
}

// get MSMM notification string
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;
}

// get connection mode string
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;
}

// get PHY type string
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;
}

// get BSS type string
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;
}

// get radio state string
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;
}


// get auth algorithm string
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;
}

// get cipher algorithm string
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;
}

// get SSID from the WCHAR string
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,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -