Recupero dei formati di servizio supportati

L'applicazione WpdServicesApiSample include codice che illustra come un'applicazione può recuperare i formati supportati da un determinato servizio Contatti chiamando metodi delle interfacce nella tabella seguente.

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

 

Quando l'utente sceglie l'opzione "3" nella riga di comando, l'applicazione richiama il metodo ListSupportedFormats 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 formato viene descritto dagli attributi che specificano il nome e (facoltativamente) il tipo MIME di un determinato formato. Questi attributi sono definiti nel file di intestazionePortableDevice.h. Per una descrizione degli attributi supportati, vedere l'argomento Attributi .

Nel caso dell'applicazione di esempio, se WpdServiceSampleDriver è l'unico dispositivo installato, il driver restituisce due formati supportati per il relativo servizio Contact: "AbstractContactFormat" e "VCard2Format". Questi formati corrispondono ai WPD_OBJECT_FORMAT_ABSTRACT_CONTACT e agli attributi WPD_OBJECT_FORMAT_VCARD2 trovati in PortableDevice.h.

Due metodi nel modulo ServiceCapabilities.cpp supportano il recupero dei formati supportati per il servizio Contatti: ListSupportedFormats e DisplayFormat. L'oggetto precedente recupera l'identificatore GUID per ogni formato supportato. Quest'ultimo converte questo GUID in una stringa descrittiva.

Il metodo ListSupportedFormats richiama il metodo IPortableDeviceService::Capabilities per recuperare un'interfaccia IPortableDeviceServiceCapabilities . Usando questa interfaccia, recupera i formati supportati chiamando il metodo IPortableDeviceServiceCapabilities::GetSupportedFormats . Il metodo GetSupportedFormats recupera il GUID per ogni formato supportato dal servizio e copia il GUID in un oggetto IPortableDevicePropVariantCollection .

Il codice seguente usa il metodo ListSupportedFormats .

// List all supported formats on the service
void ListSupportedFormats(
    IPortableDeviceService* pService)
{
    HRESULT hr              = S_OK;
    DWORD   dwNumFormats    = 0;
    CComPtr<IPortableDeviceServiceCapabilities>     pCapabilities;
    CComPtr<IPortableDevicePropVariantCollection>   pFormats;

    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 formats supported by the service.
    if (SUCCEEDED(hr))
    {
        hr = pCapabilities->GetSupportedFormats(&pFormats);
        if (FAILED(hr))
        {
            printf("! Failed to get supported formats from the service, hr = 0x%lx\n",hr);
        }
    }

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

    printf("\n%d Supported Formats Found on the service\n\n", dwNumFormats);

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

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

            PropVariantClear(&pv);
        }
    }
}

Dopo che il metodo ListSupportedFormats recupera il GUID per ogni formato supportato dal servizio specificato, richiama il metodo DisplayFormat per visualizzare il nome descrittivo dello script per ogni formato; Ad esempio, "VCard2".

Il metodo DisplayFormat richiama il metodo IPortableDeviceServiceCapabilities::GetFormatAttributes recupera una raccolta di attributi per il GUID di formato specificato. Chiama quindi il metodo IPortableDeviceValues::GetStringValue e richiede che il driver restituisca un nome descrittivo per lo script per il formato specificato.

Il codice seguente usa il metodo DisplayFormat .

// Display basic information about a format
void DisplayFormat(
    IPortableDeviceServiceCapabilities* pCapabilities,
    REFGUID                             Format)
{
    CComPtr<IPortableDeviceValues> pAttributes;

    HRESULT hr = pCapabilities->GetFormatAttributes(Format, &pAttributes);

    if (SUCCEEDED(hr))
    {
        PWSTR pszFormatName = NULL;
        hr = pAttributes->GetStringValue(WPD_FORMAT_ATTRIBUTE_NAME, &pszFormatName);

        // Display the name of the format if it is available, otherwise fall back to displaying the GUID.
        if (SUCCEEDED(hr))
        {
            printf("%ws", pszFormatName);
        }
        else
        {
            printf("%ws", (PWSTR)CGuidToString(Format));
        }       

        CoTaskMemFree(pszFormatName);
        pszFormatName = NULL;
    }
}

IPortableDeviceService

IPortableDeviceServiceCapabilities

IPortableDeviceValues

WpdServicesApiSample