Obtaining Service Objects

Device objects expose a property called Services that returns a collection of Service objects that contains one service object for each service exported by the device. Applications are able to traverse this collection sequentially, or request a particular service by using its service ID.

VBScript Example

The following example is VBScript code that extracts Service objects for two of the services exported by a device.

' Get the service objects
services = device.Services
    
Set appService = services( "urn:upnp-org:serviceId:DVDVideo" )
Set xportService = services( "urn:upnp-org:serviceId:AVTransport" )

The first line extracts the services collection from the Device object by querying the Services property. The next two lines obtain the two desired Service objects from the collection by specifying their service IDs. The services collection can also be traversed sequentially by using a for each … next loop.

C++ Example

The following example shows the C++ code required to obtain Service objects from a device. First, the sample code queries the IUPnPDevice::Services property on the interface that was passed to the function. This returns a service collection using the IUPnPServices interface. To obtain individual Service objects, use the Item method, and specify the requested service IDs. To traverse the collection sequentially, use the IEnumVARIANT::Reset, IEnumVARIANT::Next, and IEnumVARIANT::Skip methods. This example is similar to the example used to traverse the IUPnPDevices collection.

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