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

 <VBScript>

Dim deviceFinder
Set deviceFinder = CreateObject("UPnP.UPnPDeviceFinder")
Dim devices
Set devices = deviceFinder.FindByType("ssdp:all", 0)
For Each device In devices
    WScript.Echo device.FriendlyName + " " + device.Type
Next

 

Windows UPnP APIs

上一篇:WIN10 蓝牙连接音箱之后,音量调节无效,音量从1-100,声音一样大,都是最大声,可以静音(解决方案)


下一篇:使用Git提交与管理代码