서비스 개체 가져오기
디바이스 개체는 디바이스에서 내보낸 각 서비스에 대해 하나의 서비스 개체가 포함된 Service 개체의 컬렉션을 반환하는 Services 라는 속성을 노출합니다. 애플리케이션은 이 컬렉션을 순차적으로 트래버스하거나 해당 서비스 ID를 사용하여 특정 서비스를 요청할 수 있습니다.
VBScript 예제
다음 예제는 디바이스에서 내보낸 두 서비스에 대한 Service 개체를 추출하는 VBScript 코드입니다.
' Get the service objects
services = device.Services
Set appService = services( "urn:upnp-org:serviceId:DVDVideo" )
Set xportService = services( "urn:upnp-org:serviceId:AVTransport" )
첫 번째 줄은 Services 속성을 쿼리하여 Device 개체에서 서비스 컬렉션을 추출합니다. 다음 두 줄은 해당 서비스 ID를 지정하여 컬렉션에서 원하는 두 개의 Service 개체를 가져옵니다. 서비스 컬렉션은 각각에 대해 를 사용하여 순차적으로 트래버스할 수도 있습니다. 다음 루프.
C++ 예제
다음 예제에서는 디바이스에서 서비스 개체를 가져오는 데 필요한 C++ 코드를 보여줍니다. 먼저 샘플 코드는 함수에 전달된 인터페이스에서 IUPnPDevice::Services 속성을 쿼리합니다. IUPnPServices 인터페이스를 사용하여 서비스 컬렉션을 반환합니다. 개별 서비스 개체를 가져오려면 Item 메서드를 사용하고 요청된 서비스 ID를 지정합니다. 컬렉션을 순차적으로 트래버스하려면 IEnumVARIANT::Reset, IEnumVARIANT::Next 및 IEnumVARIANT::Skip 메서드를 사용합니다. 이 예제는 IUPnPDevices 컬렉션을 트래버스하는 데 사용되는 예제와 유사합니다.
#include <windows.h>
#include <upnp.h>
#pragma comment(lib, "oleaut32.lib")
HRESULT ExtractServices(IUPnPDevice * pDevice)
{
// Create a BSTR to hold the service name
BSTR bstrServiceName = SysAllocString(L"urn:upnp-org:servicId:DVDVideo");
if (NULL == bstrServiceName)
{
return E_OUTOFMEMORY;
}
// Get the list of services available on the device
IUPnPServices * pServices = NULL;
HRESULT hr = pDevice->get_Services(&pServices);
if (SUCCEEDED(hr))
{
// Retrieve the service we are interested in
IUPnPService * pAppService = NULL;
hr = pServices->get_Item(bstrServiceName, &pAppService);
if (SUCCEEDED(hr))
{
// Do something interesting with the service object
pAppService->Release();
}
pServices->Release();
}
SysFreeString(bstrServiceName);
return hr;
}