同步搜索
Device Finder 对象支持同步和异步搜索。 只有在找到所有可用设备后,同步搜索才会完成并将控制权返回给调用应用程序。 若要执行同步搜索,请使用 IUPnPDeviceFinder 接口的同步搜索方法之一。
注意
同步搜索至少需要 9 秒才能返回。 之所以出现延迟,是因为必须多次发送初始 UDP 搜索消息。 这种重复导致基础网络协议不可靠。 同步搜索最适合命令行接口。 不建议将其用于图形用户界面。
IUPnPDeviceFinder::FindByType 方法按设备或服务类型搜索。 此方法采用类型 URI 作为输入参数,并返回 Device 对象的集合。 Device 对象表示单个设备。
以下示例演示如何在 VBScript 中对设备执行同步搜索。 每个脚本使用 IUPnPDeviceFinder::FindByType,该类型使用 uri 类型调用, (服务 ID) 媒体播放器设备类型 urn:schemas-upnp-org:device:mediaplayer.v1.00.00。 方法返回对应于找到的媒体播放器设备的 Device 对象的集合。 有关从集合中提取单个设备对象的信息,请参阅 同步搜索返回的设备集合。
在 VBScript 中按类型搜索设备
Dim deviceFinder
Set deviceFinder = CreateObject( "UPnP.UPnPDeviceFinder" )
Dim devices
Set devices = deviceFinder.FindByType( "urn:schemas-upnp-org:device:multidisk-dvd", 0 )
在 C++ 中按类型搜索设备
以下示例演示了使用 C++ 对媒体播放器设备的同步搜索。 函数采用指向 IUPnPDeviceFinder 接口的指针作为输入,并返回 IUPnPDevices 接口指针。
该示例首先分配一个 BSTR 来表示设备类型 URI,然后将其传递给 IUPnPDeviceFinder::FindByType 方法。 它还在缓冲区中传递本地 IUPnPDevices 指针的地址,然后该方法在其中存储找到的设备集合。 如果函数调用成功,它将返回指向此集合的指针。
#include <windows.h>
#include <upnp.h>
#include <stdio.h>
#pragma comment(lib, "oleaut32.lib")
IUPnPDevices *FindMediaPlayerDevices(IUPnPDeviceFinder *pDeviceFinder)
{
HRESULT hr = S_OK;
IUPnPDevices * pFoundDevices = NULL;
BSTR bstrTypeURI = NULL;
bstrTypeURI =
SysAllocString(L"urn:schemas-upnp-org:device:multidisk-dvd");
if (bstrTypeURI)
{
hr = pDeviceFinder->FindByType(bstrTypeURI,
0,
&pFoundDevices);
if (SUCCEEDED(hr))
{
wprintf(L"FindMediaPlayerDevices(): "
L"Search returned successfully\n");
}
else
{
fwprintf(stderr, L"FindMediaPlayerDevices(): "
L"FindByType search failed - returned "
L"HRESULT 0x%x\n",
hr);
pFoundDevices = NULL;
}
SysFreeString(bstrTypeURI);
}
else
{
fwprintf(stderr, L"FindMediaPlayerDevices(): "
L"Could not allocate BSTR for type URI\n");
}
return pFoundDevices;
}