Windows UPnP APIs

查找设备

<C++>

#include <iostream>
#include <Windows.h>
#include <UPnP.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")

using namespace std;

int main()
{
    do
    {
        if (CoInitialize(NULL) != S_OK)
        {
            break;
        }
        IUPnPDeviceFinder *pDeviceFinder = NULL;
        if (CoCreateInstance(CLSID_UPnPDeviceFinder,
                             NULL,
                             CLSCTX_INPROC_SERVER,
                             IID_IUPnPDeviceFinder,
                             (void **)&pDeviceFinder) != S_OK)
        {
            break;
        }
        BSTR bstrSsdpAll = SysAllocString(L"ssdp:all");
        IUPnPDevices *pDevices = NULL;
        if (pDeviceFinder->FindByType(bstrSsdpAll, 0, &pDevices) != S_OK)
        {
            break;
        }
        SysFreeString(bstrSsdpAll);
        IEnumVARIANT *pEnumVar = NULL;
        if (pDevices->get__NewEnum((IUnknown **)&pEnumVar) != S_OK)
        {
            break;
        }
        if (((IUnknown *)pEnumVar)->QueryInterface(IID_IEnumVARIANT, (void **)&pEnumVar) != S_OK)
        {
            break;
        }
        VARIANT varCurDevice;
        VariantInit(&varCurDevice);
        pEnumVar->Reset();
        while (pEnumVar->Next(1, &varCurDevice, NULL) == S_OK)
        {
            IUPnPDevice *pDevice = NULL;
            IDispatch *pdispDevice = V_DISPATCH(&varCurDevice);
            if (pdispDevice->QueryInterface(IID_IUPnPDevice, (void **)&pDevice) != S_OK)
            {
                continue;
            }
            BSTR bstrName = NULL;
            BSTR bstrType = NULL;
            if (pDevice->get_FriendlyName(&bstrName) != S_OK)
            {
                continue;
            }
            pDevice->get_Type(&bstrType);
            wcout << bstrName << " " << bstrType << "\n";
            SysFreeString(bstrName);
            SysFreeString(bstrType);
        }
    } while (false);
    CoUninitialize();
}

猜你喜欢

转载自www.cnblogs.com/JebediahKerman/p/Windows_UPnP_APIs.html