Share via


Écriture de propriétés d’objet

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 WpdServicesApiSample inclut du code qui montre comment une application peut mettre à jour la propriété name pour un objet enfant du service Contacts donné. Cet exemple utilise les interfaces WPD suivantes.

Interface Description
IPortableDeviceService Utilisé pour récupérer 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 Utilisé pour écrire les valeurs de propriété d’objet et déterminer si une propriété donnée peut être écrite
IPortableDeviceValues Utilisé pour contenir les valeurs de propriété à écrire, déterminer les résultats de l’opération d’écriture et récupérer les attributs des propriétés (lors de la détermination de la capacité d’écriture).

 

Lorsque l’utilisateur choisit l’option « 8 » sur la ligne de commande, l’application appelle la méthode WriteContentProperties qui se trouve dans le module ContentProperties.cpp. Cette méthode invite l’utilisateur à entrer un identificateur d’objet pour la propriété à mettre à jour. L’utilisateur identifie l’objet et la méthode invite l’utilisateur à spécifier un nouveau nom. Une fois ce nom spécifié, la méthode met à jour la propriété Name pour l’objet donné.

Notez qu’avant d’écrire les propriétés de l’objet, l’exemple d’application ouvre un service Contacts sur un appareil connecté.

Le code suivant pour la méthode WriteContentProperties montre comment l’application utilise l’interface IPortableDeviceContent2 pour récupérer une interface IPortableDeviceProperties . En passant les PROPERTYKEYS des propriétés demandées à la méthode IPortableDeviceProperties::SetValues , WriteContentProperties met à jour la propriété name.

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

 HRESULT          hr       = S_OK;
 WCHAR         wszSelection[81]  = {0};
 WCHAR         wszNewObjectName[81] = {0};
 CComPtr<IPortableDeviceProperties> pProperties;
 CComPtr<IPortableDeviceContent2>   pContent;
 CComPtr<IPortableDeviceValues>  pObjectPropertiesToWrite;
 CComPtr<IPortableDeviceValues>  pPropertyWriteResults;
 CComPtr<IPortableDeviceValues>  pAttributes;
 BOOL          bCanWrite   = FALSE;

 // Prompt user to enter an object identifier on the device to write properties on.
 printf("Enter the identifer of the object you wish to write properties on.\n>");
 hr = StringCbGetsW(wszSelection,sizeof(wszSelection));
 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) Check the property attributes to see if we can write/change the NAME_GenericObj_Name property.
 if (SUCCEEDED(hr))
 {
  hr = pProperties->GetPropertyAttributes(wszSelection,
            PKEY_GenericObj_Name,
            &pAttributes);
  if (SUCCEEDED(hr))
  {
   hr = pAttributes->GetBoolValue(WPD_PROPERTY_ATTRIBUTE_CAN_WRITE, &bCanWrite);
   if (SUCCEEDED(hr))
   {
    if (bCanWrite)
    {
     printf("The attribute WPD_PROPERTY_ATTRIBUTE_CAN_WRITE for PKEY_GenericObj_Name reports TRUE\nThis means that the property can be changed/updated\n\n");
    }
    else
    {
     printf("The attribute WPD_PROPERTY_ATTRIBUTE_CAN_WRITE for PKEY_GenericObj_Name reports FALSE\nThis means that the property cannot be changed/updated\n\n");
    }
   }
   else
   {
    printf("! Failed to get the WPD_PROPERTY_ATTRIBUTE_CAN_WRITE value for PKEY_GenericObj_Name on object '%ws', hr = 0x%lx\n", wszSelection, hr);
   }
  }
 }

 // 4) Prompt the user for the new value of the NAME_GenericObj_Name property only if the property attributes report
 // that it can be changed/updated.
 if (bCanWrite)
 {
  printf("Enter the new PKEY_GenericObj_Name property for the object '%ws'.\n>",wszSelection);
  hr = StringCbGetsW(wszNewObjectName,sizeof(wszNewObjectName));
  if (FAILED(hr))
  {
   printf("An invalid PKEY_GenericObj_Name was specified, aborting property writing\n");
  }

  // 5) CoCreate an IPortableDeviceValues interface to hold the property values
  // we wish to write.
  if (SUCCEEDED(hr))
  {
   hr = CoCreateInstance(CLSID_PortableDeviceValues,
          NULL,
          CLSCTX_INPROC_SERVER,
          IID_IPortableDeviceValues,
          (VOID**) &pObjectPropertiesToWrite);
   if (SUCCEEDED(hr))
   {
    if (pObjectPropertiesToWrite != NULL)
    {
     hr = pObjectPropertiesToWrite->SetStringValue(PKEY_GenericObj_Name, wszNewObjectName);
     if (FAILED(hr))
     {
      printf("! Failed to add PKEY_GenericObj_Name to IPortableDeviceValues, hr= 0x%lx\n", hr);
     }
    }
   }
  }

  // 6) Call SetValues() passing the collection of specified PROPERTYKEYs.
  if (SUCCEEDED(hr))
  {
   hr = pProperties->SetValues(wszSelection,      // The object whose properties we are reading
          pObjectPropertiesToWrite,   // The properties we want to read
          &pPropertyWriteResults); // Driver supplied property result values for the property read operation
   if (FAILED(hr))
   {
    printf("! Failed to set properties for object '%ws', hr= 0x%lx\n", wszSelection, hr);
   }
   else
   {
    printf("The PKEY_GenericObj_Name property on object '%ws' was written successfully (Read the properties again to see the updated value)\n", wszSelection);
   }
  }
 }
}

IPortableDeviceContent2

IPortableDeviceProperties

WpdServicesApiSample