Dela via


Hämta servicehändelser som stöds

WpdServicesApiSample-programmet innehåller kod som visar hur ett program kan hämta händelser som stöds av en viss kontakttjänst genom att anropa metoder i följande gränssnitt.

Gränssnitt Beskrivning
IPortableDeviceService Används för att hämta gränssnittet IPortableDeviceServiceCapabilities för att komma åt de händelser som stöds.
IPortableDeviceServiceCapabilities Ger åtkomst till händelser och händelseattribut som stöds.
IPortableDevicePropVariantCollection Innehåller listan över händelser som stöds.
IPortableDeviceValues Innehåller attributen för en viss händelse.

 

När användaren väljer alternativet "4" på kommandoraden anropar programmet ListSupportedEvents metod som finns i modulen ServiceCapabilities.cpp.

Observera att innan du hämtar listan över händelser öppnar exempelprogrammet en kontakttjänst på en ansluten enhet.

I WPD beskrivs en händelse med dess namn, alternativ och parametrar. Händelsenamnet är en skriptvänlig sträng, till exempel "MyCustomEvent". Händelsealternativen anger om en viss händelse sänds till alla klienter och om händelsen stöder automatisk uppspelning. Attributen för händelseparametern inkluderar PROPERTYKEY och VARTYPE för en viss parameter.

Fyra metoder i modulen ServiceCapabilities.cpp stöder hämtning av händelser som stöds för den angivna kontakttjänsten: ListSupportedEvents, DisplayEvent, DisplayEventOptionsoch DisplayEventParameters. Metoden ListSupportedEvents hämtar antalet händelser som stöds och GUID-identifieraren för varje händelse. Metoden DisplayEvent visar händelsenamnet eller GUID och anropar sedan DisplayEventOptions och DisplayEventParameters för att visa händelserelaterade data.

Metoden ListSupportedEvents anropar metoden IPortableDeviceService::Capabilities för att hämta ett IPortableDeviceServiceCapabilities--gränssnitt. Med det här gränssnittet hämtar den de händelser som stöds genom att anropa metoden IPortableDeviceServiceCapabilities::GetSupportedEvents. Metoden GetSupportedEvents hämtar GUID:erna för varje händelse som stöds av tjänsten och kopierar dessa GUID:er till ett IPortableDevicePropVariantCollection- objekt.

Följande kod visar hur du hämtar tjänsthändelser som stöds.

// List all supported events on the service
void ListSupportedEvents(
    IPortableDeviceService* pService)
{
    HRESULT hr          = S_OK;
    DWORD   dwNumEvents = 0;
    CComPtr<IPortableDeviceServiceCapabilities>     pCapabilities;
    CComPtr<IPortableDevicePropVariantCollection>   pEvents;

    if (pService == NULL)
    {
        printf("! A NULL IPortableDeviceService interface pointer was received\n");
        return;
    }

    // Get an IPortableDeviceServiceCapabilities interface from the IPortableDeviceService interface to
    // access the service capabilities-specific methods.
    hr = pService->Capabilities(&pCapabilities);
    if (FAILED(hr))
    {
        printf("! Failed to get IPortableDeviceServiceCapabilities from IPortableDeviceService, hr = 0x%lx\n",hr);
    }

    // Get all events supported by the service.
    if (SUCCEEDED(hr))
    {
        hr = pCapabilities->GetSupportedEvents(&pEvents);
        if (FAILED(hr))
        {
            printf("! Failed to get supported events from the service, hr = 0x%lx\n",hr);
        }
    }

    // Get the number of supported events found on the service.
    if (SUCCEEDED(hr))
    {
        hr = pEvents->GetCount(&dwNumEvents);
        if (FAILED(hr))
        {
            printf("! Failed to get number of supported events, hr = 0x%lx\n",hr);
        }
    }

    printf("\n%d Supported Events Found on the service\n\n", dwNumEvents);

    // Loop through each event and display it
    if (SUCCEEDED(hr))
    {
        for (DWORD dwIndex = 0; dwIndex < dwNumEvents; dwIndex++)
        {
            PROPVARIANT pv = {0};
            PropVariantInit(&pv);
            hr = pEvents->GetAt(dwIndex, &pv);

            if (SUCCEEDED(hr))
            {
                // We have an event.  It is assumed that
                // events are returned as VT_CLSID VarTypes.
                if (pv.puuid != NULL)
                {
                    DisplayEvent(pCapabilities, *pv.puuid);
                    printf("\n");
                }
            }

            PropVariantClear(&pv);
        }
    }
}

När metoden ListSupportedEvents hämtar GUID:erna som representerar varje händelse som stöds av den angivna tjänsten anropas metoden DisplayEvent för att hämta händelsespecifika data. Dessa data innehåller händelsenamnet, dess alternativ (sändning eller automatisk uppspelning), parameter-GUID, parametertyper och så vidare.

Metoden DisplayEvent anropar metoden IPortableDeviceServiceCapabilities::GetEventAttributes metod för att hämta en samling attribut för den angivna händelsen. Den anropar sedan metoden IPortableDeviceValues::GetStringValue och begär att drivrutinen returnerar ett användarvänligt namn för den angivna händelsen. Nästa steg, DisplayEvent anropar IPortableDeviceValues::GetIPortableDeviceValuesValue för att hämta händelsealternativen. Slutligen anropar DisplayEvent IPortableDeviceValues::GetIPortableDeviceKeyCollectionValue för att hämta listan över händelseparametrar. Den skickar data som returneras av dessa metoder till DisplayEventOptions och DisplayEventParameters hjälpfunktioner, som återger alternativ och parameterinformation för den angivna händelsen.

Följande kod använder metoden DisplayEvent.

// Display basic information about an event
void DisplayEvent(
    IPortableDeviceServiceCapabilities* pCapabilities,
    REFGUID                             Event)
{
    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the event attributes which describe the event
    HRESULT hr = pCapabilities->GetEventAttributes(Event, &pAttributes);
    if (FAILED(hr))
    {
        printf("! Failed to get the event attributes, hr = 0x%lx\n",hr);
    }

    if (SUCCEEDED(hr))
    {
        PWSTR pszFormatName = NULL;
        CComPtr<IPortableDeviceValues>          pOptions;
        CComPtr<IPortableDeviceKeyCollection>   pParameters;

        // Display the name of the event if it is available. Otherwise, fall back to displaying the GUID.
        hr = pAttributes->GetStringValue(WPD_EVENT_ATTRIBUTE_NAME, &pszFormatName);
        if (SUCCEEDED(hr))
        {
            printf("%ws\n", pszFormatName);
        }
        else
        {
            printf("%ws\n", (PWSTR)CGuidToString(Event));
        }       

        // Display the event options
        hr = pAttributes->GetIPortableDeviceValuesValue(WPD_EVENT_ATTRIBUTE_OPTIONS, &pOptions);
        if (SUCCEEDED(hr))
        {
            DisplayEventOptions(pOptions);
        }
        else
        {
            printf("! Failed to get the event options, hr = 0x%lx\n", hr);
        }       

        // Display the event parameters
        hr = pAttributes->GetIPortableDeviceKeyCollectionValue(WPD_EVENT_ATTRIBUTE_PARAMETERS, &pParameters);
        if (SUCCEEDED(hr))
        {
            DisplayEventParameters(pCapabilities, Event, pParameters);
        }
        else
        {
            printf("! Failed to get the event parameters, hr = 0x%lx\n", hr);
        }       
        
        CoTaskMemFree(pszFormatName);
        pszFormatName = NULL;
    }
}

Hjälpfunktionen DisplayEventOptions tar emot ett IPortableDeviceValues- objekt som innehåller händelsens alternativdata. Den anropar sedan metoden IPortableDeviceValues::GetBoolValue för att hämta alternativdata. Med hjälp av dessa data återges en sträng som anger om alternativen för sändning och automatisk uppspelning stöds.

// Display the basic event options.
void DisplayEventOptions(
    IPortableDeviceValues* pOptions)
{
    printf("\tEvent Options:\n");
    // Read the WPD_EVENT_OPTION_IS_BROADCAST_EVENT value to see if the event is
    // a broadcast event. If the read fails, assume FALSE
    BOOL  bIsBroadcastEvent = FALSE;
    pOptions->GetBoolValue(WPD_EVENT_OPTION_IS_BROADCAST_EVENT, &bIsBroadcastEvent);
    printf("\tWPD_EVENT_OPTION_IS_BROADCAST_EVENT = %ws\n",bIsBroadcastEvent ? L"TRUE" : L"FALSE");
}

IPortableDeviceKeyCollection

IPortableDeviceService

IPortableDeviceServiceCapabilities

IPortableDeviceValues

WpdServicesApiSample