Share via


Récupération des propriétés de l’objet WPD

Les services contiennent souvent des objets enfants qui appartiennent à l’un des formats pris en charge par chaque service. Par exemple, un service Contacts peut prendre en charge plusieurs objets contact au format Contact abstrait. Chaque objet contact est décrit par des propriétés associées (nom du contact, numéro de téléphone, adresse e-mail, etc.).

L’application WpdServiceApiSample inclut du code qui montre comment une application peut récupérer les propriétés content-object prises en charge par un service Contacts donné. Cet exemple utilise les interfaces suivantes.

Interface Description
IPortableDeviceService Récupère l’interface IPortableDeviceContent2 pour accéder aux méthodes de service prises en charge.
IPortableDeviceContent2 Fournit l’accès aux méthodes spécifiques au contenu.
IPortableDeviceProperties Récupère les valeurs de propriété de l’objet.
IPortableDeviceValues Contient les valeurs de propriété qui ont été lues pour cet objet.
IPortableDeviceKeyCollection Contient les paramètres d’une méthode donnée.

 

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

Cette méthode récupère les quatre propriétés suivantes pour l’objet contact spécifié.

Propriété Description Device Services PROPERTYKEY WPD_PROPERTYKEY équivalent
Identificateur d’objet parent Chaîne qui spécifie l’identificateur du parent de l’objet donné. PKEY_GenericObj_ParentID WPD_OBJECT_PARENT_ID
Nom d’objet Chaîne qui spécifie le nom de l’objet donné PKEY_GenericObj_Name WPD_OBJECT_NAME
Identificateur unique persistant Chaîne qui spécifie un identificateur unique pour l’objet donné. Cet identificateur est persistant entre les sessions, contrairement à l’identificateur d’objet. Pour les services, il doit s’agir d’une représentation sous forme de chaîne d’un GUID. PKEY_GenericObj_PersistentUID WPD_OBJECT_PERSISTENT_UNIQUE_ID
Format d’objet Identificateur global unique (GUID) qui spécifie le format du fichier correspondant à un objet donné PKEY_GenericObj_ObjectFormat WPD_OBJECT_FORMAT

 

  • Parent ID
  • Nom
  • PERSISTENTUID
  • Format

Notez qu’avant de récupérer les propriétés de contenu, l’exemple d’application ouvre un service Contacts sur un appareil connecté.

Le code suivant pour la méthode ReadContentProperties montre comment l’application utilise l’interface IPortableDeviceContent2 pour récupérer une interface IPortableDeviceProperties . En transmettant les PROPERTYKEYS des propriétés demandées à la méthode IPortableDeviceProperties::GetValues , ReadContentProperties récupère les valeurs demandées, puis les affiche dans la fenêtre de console.

// Reads properties for the user specified object.
void ReadContentProperties(
    IPortableDeviceService*    pService)
{
    if (pService == NULL)
    {
        printf("! A NULL IPortableDeviceService interface pointer was received\n");
        return;
    }

    HRESULT                               hr              = S_OK;
    WCHAR                                 szSelection[81] = {0};
    CComPtr<IPortableDeviceProperties>    pProperties;
    CComPtr<IPortableDeviceValues>        pObjectProperties;
    CComPtr<IPortableDeviceContent2>      pContent;
    CComPtr<IPortableDeviceKeyCollection> pPropertiesToRead;

    // Prompt user to enter an object identifier on the device to read properties from.
    printf("Enter the identifer of the object you wish to read properties from.\n>");
    hr = StringCbGetsW(szSelection,sizeof(szSelection));
    if (FAILED(hr))
    {
        printf("An invalid object identifier was specified, aborting property reading\n");
    }

    // 1) Get an IPortableDeviceContent2 interface from the IPortableDeviceService interface to
    // access the content-specific methods.
    if (SUCCEEDED(hr))
    {
        hr = pService->Content(&pContent);
        if (FAILED(hr))
        {
            printf("! Failed to get IPortableDeviceContent2 from IPortableDeviceService, hr = 0x%lx\n",hr);
        }
    }

    // 2) Get an IPortableDeviceProperties interface from the IPortableDeviceContent2 interface
    // to access the property-specific methods.
    if (SUCCEEDED(hr))
    {
        hr = pContent->Properties(&pProperties);
        if (FAILED(hr))
        {
            printf("! Failed to get IPortableDeviceProperties from IPortableDeviceContent2, hr = 0x%lx\n",hr);
        }
    }

    // 3) CoCreate an IPortableDeviceKeyCollection interface to hold the property keys
    // we wish to read.
    hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_PPV_ARGS(&pPropertiesToRead));
    if (SUCCEEDED(hr))
    {
        // 4) Populate the IPortableDeviceKeyCollection with the keys we wish to read.
        // NOTE: We are not handling any special error cases here so we can proceed with
        // adding as many of the target properties as we can.
        if (pPropertiesToRead != NULL)
        {
            HRESULT hrTemp = S_OK;
            hrTemp = pPropertiesToRead->Add(PKEY_GenericObj_ParentID);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add PKEY_GenericObj_ParentID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

            hrTemp = pPropertiesToRead->Add(PKEY_GenericObj_Name);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add PKEY_GenericObj_Name to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

            hrTemp = pPropertiesToRead->Add(PKEY_GenericObj_PersistentUID);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add PKEY_GenericObj_PersistentUID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

            hrTemp = pPropertiesToRead->Add(PKEY_GenericObj_ObjectFormat);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add PKEY_GenericObj_ObjectFormat to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

        }
    }

    // 5) Call GetValues() passing the collection of specified PROPERTYKEYs.
    if (SUCCEEDED(hr))
    {
        hr = pProperties->GetValues(szSelection,         // The object whose properties we are reading
                                    pPropertiesToRead,   // The properties we want to read
                                    &pObjectProperties); // Driver supplied property values for the specified object
        if (FAILED(hr))
        {
            printf("! Failed to get all properties for object '%ws', hr= 0x%lx\n", szSelection, hr);
        }
    }

    // 6) Display the returned property values to the user
    if (SUCCEEDED(hr))
    {
        DisplayStringProperty(pObjectProperties, PKEY_GenericObj_ParentID,        NAME_GenericObj_ParentID);
        DisplayStringProperty(pObjectProperties, PKEY_GenericObj_Name,            NAME_GenericObj_Name);
        DisplayStringProperty(pObjectProperties, PKEY_GenericObj_PersistentUID,   NAME_GenericObj_PersistentUID);
        DisplayGuidProperty  (pObjectProperties, PKEY_GenericObj_ObjectFormat,    NAME_GenericObj_ObjectFormat);
    }
}

IPortableDeviceContent2

IPortableDeviceProperties

WpdServicesApiSample