Share via


Récupération des formats de service pris en charge

L’application WpdServicesApiSample inclut du code qui montre comment une application peut récupérer les formats pris en charge par un service Contacts donné en appelant les méthodes des interfaces dans le tableau suivant.

Interface Description
IPortableDeviceService Permet de récupérer l’interface IPortableDeviceServiceCapabilities pour accéder aux événements pris en charge.
IPortableDeviceServiceCapabilities Fournit l’accès aux événements et attributs d’événement pris en charge.
IPortableDevicePropVariantCollection Contient la liste des formats pris en charge.
IPortableDeviceValues Contient les attributs d’un format donné.

 

Lorsque l’utilisateur choisit l’option « 3 » sur la ligne de commande, l’application appelle la méthode ListSupportedFormats qui se trouve dans le module ServiceCapabilities.cpp.

Notez qu’avant de récupérer la liste des événements, l’exemple d’application ouvre un service Contacts sur un appareil connecté.

Dans WPD, un format est décrit par des attributs qui spécifient le nom et (éventuellement) le type MIME d’un format donné. Ces attributs sont définis dans le fichier d’en-têtePortableDevice.h. Pour obtenir une description des attributs pris en charge, reportez-vous à la rubrique Attributs .

Dans le cas de l’exemple d’application, si WpdServiceSampleDriver est le seul appareil installé, le pilote retourne deux formats pris en charge pour son service Contact : « AbstractContactFormat » et « VCard2Format ». Ces formats correspondent aux attributs WPD_OBJECT_FORMAT_ABSTRACT_CONTACT et WPD_OBJECT_FORMAT_VCARD2 trouvés dans PortableDevice.h.

Deux méthodes du module ServiceCapabilities.cpp prennent en charge la récupération des formats pris en charge pour le service Contacts : ListSupportedFormats et DisplayFormat. Le premier récupère l’identificateur GUID pour chaque format pris en charge. Ce dernier convertit ce GUID en chaîne conviviale.

La méthode ListSupportedFormats appelle la méthode IPortableDeviceService::Capabilities pour récupérer une interface IPortableDeviceServiceCapabilities . À l’aide de cette interface, il récupère les formats pris en charge en appelant la méthode IPortableDeviceServiceCapabilities::GetSupportedFormats . La méthode GetSupportedFormats récupère le GUID pour chaque format pris en charge par le service et copie ce GUID dans un objet IPortableDevicePropVariantCollection .

Le code suivant utilise la méthode 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);
        }
    }
}

Une fois que la méthode ListSupportedFormats a récupéré le GUID pour chaque format pris en charge par le service donné, elle appelle la méthode DisplayFormat pour afficher le nom convivial du script pour chaque format ; par exemple, « VCard2 ».

La méthode DisplayFormat appelle la méthode IPortableDeviceServiceCapabilities::GetFormatAttributes pour récupérer une collection d’attributs pour le GUID de format donné. Il appelle ensuite la méthode IPortableDeviceValues::GetStringValue et demande que le pilote retourne un nom adapté aux scripts pour le format donné.

Le code suivant utilise la méthode 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