Recupero di eventi di servizio supportati

L'applicazione WpdServicesApiSample include codice che illustra come un'applicazione può recuperare gli eventi supportati da un determinato servizio Contatti chiamando i metodi nelle interfacce seguenti.

Interfaccia Descrizione
IPortableDeviceService Usato per recuperare l'interfaccia IPortableDeviceServiceCapabilities per accedere agli eventi supportati.
IPortableDeviceServiceCapabilities Fornisce l'accesso agli eventi e agli attributi dell'evento supportati.
IPortableDevicePropVariantCollection Contiene l'elenco degli eventi supportati.
IPortableDeviceValues Contiene gli attributi per un determinato evento.

 

Quando l'utente sceglie l'opzione "4" nella riga di comando, l'applicazione richiama il metodo ListSupportedEvents presente nel modulo ServiceCapabilities.cpp.

Si noti che prima di recuperare l'elenco di eventi, l'applicazione di esempio apre un servizio Contatti in un dispositivo connesso.

In WPD un evento viene descritto in base al nome, alle opzioni e ai parametri. Il nome dell'evento è una stringa descrittiva dello script, ad esempio "MyCustomEvent". Le opzioni dell'evento indicano se un determinato evento viene trasmesso a tutti i client e se tale evento supporta la riproduzione automatica. Gli attributi del parametro di evento includono PROPERTYKEY e VARTYPE del parametro specificato.

Quattro metodi nel modulo ServiceCapabilities.cpp supportano il recupero di eventi supportati per il servizio Contatti specificato: ListSupportedEvents, DisplayEvent, DisplayEventOptions e DisplayEventParameters. Il metodo ListSupportedEvents recupera un conteggio degli eventi supportati e l'identificatore GUID per ogni evento. Il metodo DisplayEvent visualizza il nome dell'evento o il GUID e quindi chiama DisplayEventOptions e DisplayEventParameters per visualizzare i dati correlati all'evento.

Il metodo ListSupportedEvents richiama il metodo IPortableDeviceService::Capabilities per recuperare un'interfaccia IPortableDeviceServiceCapabilities . Usando questa interfaccia, recupera gli eventi supportati chiamando il metodo IPortableDeviceServiceCapabilities::GetSupportedEvents . Il metodo GetSupportedEvents recupera i GUID per ogni evento supportato dal servizio e copia tali GUID in un oggetto IPortableDevicePropVariantCollection .

Il codice seguente illustra il recupero degli eventi del servizio supportati.

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

Dopo che il metodo ListSupportedEvents recupera i GUID che rappresentano ogni evento supportato dal servizio specificato, richiama il metodo DisplayEvent per recuperare i dati specifici dell'evento. Questi dati includono il nome dell'evento, le relative opzioni (broadcast o autoplay), i GUID dei parametri, i tipi di parametro e così via.

Il metodo DisplayEvent richiama il metodo IPortableDeviceServiceCapabilities::GetEventAttributes per recuperare una raccolta di attributi per l'evento specificato. Chiama quindi il metodo IPortableDeviceValues::GetStringValue e richiede che il driver restituisca un nome descrittivo per l'evento specificato. Successivamente , DisplayEvent chiama IPortableDeviceValues::GetIPortableDeviceValuesValue per recuperare le opzioni dell'evento. Infine, DisplayEvent chiama IPortableDeviceValues::GetIPortableDeviceKeyCollectionValue per recuperare l'elenco dei parametri dell'evento. Passa i dati restituiti da questi metodi alle funzioni helper DisplayEventOptions e DisplayEventParameters , che eseguono il rendering delle opzioni e delle informazioni sui parametri per l'evento specificato.

Il codice seguente usa il metodo 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;
    }
}

La funzione helper DisplayEventOptions riceve un oggetto IPortableDeviceValues che contiene i dati delle opzioni dell'evento. Chiama quindi il metodo IPortableDeviceValues::GetBoolValue per recuperare i dati delle opzioni. Utilizzando questi dati, viene eseguito il rendering di una stringa che indica se le opzioni broadcast e autoplay sono supportate.

// 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