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

📄 wlsample.cpp

📁 VC++ WIFI
💻 CPP
📖 第 1 页 / 共 5 页
字号:
            dwError = ERROR_INVALID_PARAMETER;
            __leave;
        }

        // open handle
        if ((dwError = OpenHandleAndCheckVersion(
                            &hClient
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }
        
        
        if (( dwError = WlanGetInterfaceCapability(
                            hClient, 
                            &guidIntf, 
                            NULL,               // reserved
                            &pCapability
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // print interface capability information
        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;
        }
        
        // print supported PHY type
        wcout << L"Supports " << pCapability->dwNumberOfSupportedPhys << L" PHY types:" << endl;
        for (i = 0; i < pCapability->dwNumberOfSupportedPhys; i++)
        {
            wcout << L"\t" << GetPhyTypeString(pCapability->dot11PhyTypes[i]) << endl;
        }

        // query supported auth/cipher for infrastructure
        if ((dwError = WlanQueryInterface(
                        hClient,
                        &guidIntf,
                        wlan_intf_opcode_supported_infrastructure_auth_cipher_pairs,
                        NULL,                   // reserved
                        &dwDataSize,
                        (PVOID *)&(pSupportedAuthCipherList),
                        NULL                    // not interesed in the type of the opcode value
                        )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // print auth/cipher algorithms
        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;

        // query supported auth/cipher for ad hoc
        if ((dwError = WlanQueryInterface(
                        hClient,
                        &guidIntf,
                        wlan_intf_opcode_supported_adhoc_auth_cipher_pairs,
                        NULL,                   // reserved
                        &dwDataSize,
                        (PVOID *)&(pSupportedAuthCipherList),
                        NULL                    // not interesed in the type of the opcode value
                        )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // print auth/cipher algorithms
        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
    {
        // clean up
        if (hClient != NULL)
        {
            WlanCloseHandle(
                hClient, 
                NULL            // reserved
                );
        }
    }

    PrintErrorMsg(argv[0], dwError);
}

// set the radio state
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;
        }
        
        // get the interface GUID
        if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
        {
            wcerr << L"Invalid GUID " << argv[1] << endl;
            dwError = ERROR_INVALID_PARAMETER;
            __leave;
        }

        // open handle
        if ((dwError = OpenHandleAndCheckVersion(
                            &hClient
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // get interface capability, which includes the supported PHYs
        if ((dwError = WlanGetInterfaceCapability(
                    hClient,
                    &guidIntf,
                    NULL,                       // reserved
                    &pInterfaceCapability
                    )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // set radio state on every PHY
        for (i = 0; i < pInterfaceCapability->dwNumberOfSupportedPhys; i++)
        {
            // set radio state on every PHY
            wlanPhyRadioState.dwPhyIndex = i;

            if ((dwError = WlanSetInterface(
                            hClient, 
                            &guidIntf, 
                            wlan_intf_opcode_radio_state, 
                            sizeof(wlanPhyRadioState),
                            (PBYTE)&wlanPhyRadioState,
                            NULL                        // reserved
                            )) != ERROR_SUCCESS)
            {
                // rollback is nice to have, but not required
                __leave;
            }
        }

    }
    __finally
    {
        // clean up
        if (hClient != NULL)
        {
            WlanCloseHandle(
                hClient, 
                NULL            // reserved
                );
        }

        if (pInterfaceCapability != NULL)
        {
            WlanFreeMemory(pInterfaceCapability);
        }
    }

    PrintErrorMsg(argv[0], dwError);
}

// query basic interface information
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;
        }

        // get the interface GUID
        if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
        {
            wcerr << L"Invalid GUID " << argv[1] << endl;
            dwError = ERROR_INVALID_PARAMETER;
            __leave;
        }

        // open handle
        if ((dwError = OpenHandleAndCheckVersion(
                            &hClient
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // query radio state information
        // this opcode is not supported in XP
        if ((dwError = WlanQueryInterface(
                        hClient,
                        &guidIntf,
                        wlan_intf_opcode_radio_state,
                        NULL,                       // reserved
                        &dwDataSize,
                        &pData,
                        NULL                        // not interesed in the type of the opcode value
                        )) != 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);

            // print radio state
            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
        {
            // not supported in XP
            // print message
            wcout << L"Querying radio state is not supported." << endl;
        }

        // query interface state
        if ((dwError = WlanQueryInterface(
                        hClient,
                        &guidIntf,
                        wlan_intf_opcode_interface_state,
                        NULL,                       // reserved
                        &dwDataSize,
                        &pData,
                        NULL                        // not interesed in the type of the opcode value
                        )) != ERROR_SUCCESS)
        {
            __leave;
        }

        if (dwDataSize != sizeof(WLAN_INTERFACE_STATE))
        {
            dwError = ERROR_INVALID_DATA;
            __leave;
        }
        
        isState = *((PWLAN_INTERFACE_STATE)pData);
        
        // print interface state
        wcout << L"Interface state: " << GetInterfaceStateString(isState) << L"." << endl;

        WlanFreeMemory(pData);
        pData = NULL;

        // query the current connection
        if ((dwError = WlanQueryInterface(
                        hClient,
                        &guidIntf,
                        wlan_intf_opcode_current_connection,
                        NULL,                       // reserved
                        &dwDataSize,
                        &pData,
                        NULL                        // not interesed in the type of the opcode value
                        )) == ERROR_SUCCESS && 
              dwDataSize == sizeof(WLAN_CONNECTION_ATTRIBUTES)
            )
        {
            pCurrentNetwork = (PWLAN_CONNECTION_ATTRIBUTES)pData;
        }

        // we don't treat ERROR_INVALID_STATE as an error for querying the interface
        if (dwError == ERROR_INVALID_STATE)
        {
            dwError = ERROR_SUCCESS;
        }
        
        if (pCurrentNetwork == NULL)
        {
            // no connection information
            __leave;
        }
        
        // print current connection information
        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);
        }
        
        // clean up
        if (hClient != NULL)
        {
            WlanCloseHandle(
                hClient, 
                NULL            // reserved
                );
        }
    }

    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;
        }

        // get the interface GUID
        if (UuidFromString((RPC_WSTR)argv[1], &guidIntf) != RPC_S_OK)
        {
            wcerr << L"Invalid GUID " << argv[1] << endl;
            dwError = ERROR_INVALID_PARAMETER;
            __leave;
        }

        // open handle
        if ((dwError = OpenHandleAndCheckVersion(
                            &hClient
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // scan
        dwError = WlanScan(
                    hClient, 
                    &guidIntf, 
                    NULL,                   // don't perform additional probe for a specific SSID
                    NULL,                   // no IE data for the additional probe
                    NULL                    // reserved
                    );

    }
    __finally
    {
        // clean up

⌨️ 快捷键说明

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