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

📄 wlsample.cpp

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

        hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
        if (hr != S_OK)
        {
            dwError = WIN32_FROM_HRESULT(hr);
            break;
        }
        
        // create a COM object to read the XML file
        hr = CoCreateInstance(
                CLSID_DOMDocument60,
                NULL,
                CLSCTX_INPROC_SERVER,
                IID_IXMLDOMDocument2,
                (void**)&pXmlDoc
                );
        if (hr != S_OK)
        {
            dwError = WIN32_FROM_HRESULT(hr);
            break;
        }
		
		// load the file into the COM object
		hr = pXmlDoc->load((CComVariant)argv[2], &vbSuccess);
        if (hr != S_OK || vbSuccess != VARIANT_TRUE)
        {
            dwError = ERROR_BAD_PROFILE;
            break;
        }

        // get XML string out from the file
        hr = pXmlDoc->get_xml(&bstrXml);
        if (hr != S_OK)
        {
            dwError = ERROR_BAD_PROFILE;
            break;
        }

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

        // set profile
        dwError = WlanSetProfile(
                            hClient, 
                            &guidIntf, 
                            0,          // no flags for the profile 
                            bstrXml, 
                            NULL,       // use the default ACL
                            TRUE,		// overwrite a profile if it already exists
                            NULL,       // reserved
                            &dwReason
                            );
        if (dwError == ERROR_BAD_PROFILE)
        {
            wcout << L"The profile is bad.";
            PrintReason(dwReason);
        }
    } while (FALSE);

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

    PrintErrorMsg(argv[0], dwError);
}

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

        // 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 profile
        if ((dwError = WlanGetProfile(
                            hClient, 
                            &guidIntf, 
                            argv[2],    // profile name
                            NULL,       // reserved
                            &strXml,    // XML string of the profile
                            NULL,       // not interested in the profile flags
                            NULL        // don't care about ACL
                            )) == ERROR_SUCCESS)
        {
            wcout << L"The return profile xml is: " << endl << strXml << endl;
            WlanFreeMemory(strXml);
        }

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

    PrintErrorMsg(argv[0], dwError);
}

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

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

        // delete profile
        dwError = WlanDeleteProfile(
                        hClient, 
                        &guidIntf, 
                        argv[2],        // profile name
                        NULL            // reserved
                        );

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

    PrintErrorMsg(argv[0], dwError);
}

// set profile list
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;
        }
            
        // 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;
        }
        
        // set profile list
        dwError = WlanSetProfileList(
                        hClient, 
                        &guidIntf, 
                        argc - 2,                   // number of profiles
                        (LPCWSTR *)(argv + 2),      // the list of profiles name following the command and the interface GUID
                        NULL                        // reserved
                        );

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

    PrintErrorMsg(argv[0], dwError);
}

// get the list of profiles
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;
        }

        // 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 profile list
        if ((dwError = WlanGetProfileList(
                            hClient, 
                            &guidIntf, 
                            NULL,               // reserved
                            &pProfileList
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        wcout << L"There are " << pProfileList->dwNumberOfItems << L" profiles on the interface." << endl;
        // print out profiles
        for (i = 0; i < pProfileList->dwNumberOfItems; i++)
        {
            pInfo = &pProfileList->ProfileInfo[i];
            wcout << L"\t\"" << pInfo->strProfileName << L"\"" << endl;
        }

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

    PrintErrorMsg(argv[0], dwError);
}

// enumerate wireless interfaces
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;
        }

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

        // enumerate wireless interfaces
        if ((dwError = WlanEnumInterfaces(
                            hClient,
                            NULL,               // reserved
                            &pIntfList
                            )) != ERROR_SUCCESS)
        {
            __leave;
        }

        wcout << L"There are " << pIntfList->dwNumberOfItems << L" interfaces in the system." << endl;

        // print out interface information
        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
    {
        // clean up
        if (pIntfList != NULL)
        {
            WlanFreeMemory(pIntfList);
        }
        
        if (hClient != NULL)
        {
            WlanCloseHandle(
                hClient, 
                NULL            // reserved
                );
        }
    }

    PrintErrorMsg(argv[0], dwError);
}

// get interface capability and supported auth/cipher
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;
        }

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

⌨️ 快捷键说明

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