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

📄 wlsample.cpp

📁 VC++ WIFI
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        if (hClient != NULL)
        {
            WlanCloseHandle(
                hClient, 
                NULL            // reserved
                );
        }
    }

    PrintErrorMsg(argv[0], dwError);
}

// get the list of visible wireless networks
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;
        }

        // 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;
        }
        
        if ((dwError = WlanGetAvailableNetworkList(
                            hClient,
                            &guidIntf,
                            0,                      // only show visible networks
                            NULL,                   // reserved
                            &pVList
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

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

    PrintErrorMsg(argv[0], dwError);
}

// get driver statistics
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;
        }

        // 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;
        }
        
        
        if ((dwError = WlanQueryInterface(
                            hClient,
                            &guidIntf,
                            wlan_intf_opcode_statistics,
                            NULL,                       // reserved
                            &dwSize,
                            &pData,
                            NULL                        // not interesed in the type of the opcode value
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        pStatistics = (PWLAN_STATISTICS)pData;
        // print statistics information
        wcout << L"Four way handshake failures: \t";
        PrintCounterValue(pStatistics->ullFourWayHandshakeFailures);

        wcout << L"TKIP Counter Measures invoked: \t";
        PrintCounterValue(pStatistics->ullTKIPCounterMeasuresInvoked);

        // frame statistics
        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);

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

    PrintErrorMsg(argv[0], dwError);
}

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

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

        if (argc == 5)
        {
            // get SSID
            if ((dwError = StringWToSsid(argv[2], &dot11Ssid)) != ERROR_SUCCESS)
            {
                __leave;
            }

            pDot11Ssid = &dot11Ssid;

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

            // get whether security enabled or not
            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;
            }
        }
        
        // open handle
        if ((dwError = OpenHandleAndCheckVersion(
                            &hClient
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }
        
        if ((dwError = WlanGetNetworkBssList(
                            hClient,
                            &guidIntf,
                            pDot11Ssid,
                            dot11BssType,
                            bSecurityEnabled,
                            NULL,                       // reserved
                            &pWlanBssList
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        for (i = 0; i < pWlanBssList->dwNumberOfItems; i++)
        {
            PrintBssInfo(&pWlanBssList->wlanBssEntries[i]);
        }
            
        WlanFreeMemory(pWlanBssList);
    }
    __finally
    {
        // clean up
        if (hClient != NULL)
        {
            WlanCloseHandle(
                hClient, 
                NULL            // reserved
                );
        }
    }

    PrintErrorMsg(argv[0], dwError);
}

// connect to a network using a saved profile
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;
        }

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

        // get SSID
        if ((dwError = StringWToSsid(argv[2], &dot11Ssid)) != ERROR_SUCCESS)
        {
            __leave;
        }

        // set the connection mode (connecting using a profile)
        wlanConnPara.wlanConnectionMode = wlan_connection_mode_profile;
        // set the profile name
        wlanConnPara.strProfile = argv[4];
        // set the SSID
        wlanConnPara.pDot11Ssid = &dot11Ssid;

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

        // the desired BSSID list is empty
        wlanConnPara.pDesiredBssidList = NULL;
        // no connection flags
        wlanConnPara.dwFlags = 0;

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

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

    PrintErrorMsg(argv[0], dwError);
}

// discovery a network without using a saved profile
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;
        }

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

        // get SSID
        if ((dwError = StringWToSsid(argv[2], &dot11Ssid)) != ERROR_SUCCESS)
        {
            __leave;
        }

        // profile is ignored for discovery
        wlanConnPara.strProfile = NULL;
        // set the SSID
        wlanConnPara.pDot11Ssid = &dot11Ssid;

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

        // get whether security enabled or not
        if (_wcsicmp(argv[4], L"secure") == 0 || _wcsicmp(argv[4], L"s") == 0)
         

⌨️ 快捷键说明

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