Share via


Setting Properties for a Single Object

banner art

Previous Next

Setting Properties for a Single Object

After your application retrieves an object identifier (see the Enumerating Content topic) for a given object, it can set properties for that object by calling methods in the interfaces described in the following table.

Interface Description
IPortableDeviceProperties Interface Used to determine whether a given property can be written.
IPortableDeviceContent Interface Provides access to the content-specific methods.
IPortableDeviceValues Interface Used to hold the values to be written, determine results of the write operation, and retrieve attributes of properties (when determining write capability).

Applications set properties on an object by first creating a property bag that specifies the new values using the IPortableDeviceValues interface. Once the property bag is created, an application sets those properties by calling the IPortableDeviceProperties::SetValues method.

The WriteContentProperties function in the sample application's ContentProperties.cpp module demonstrates how an application could set a new object-name property for a selected object.

The first task accomplished by the WriteContentProperties function is to prompt the user to enter an object identifier for the object that the application will write the new name for.

HRESULT                               hr                   = S_OK;
WCHAR                                 wszSelection[81]     = {0};
WCHAR                                 wszNewObjectName[81] = {0};
CComPtr<IPortableDeviceProperties>    pProperties;
CComPtr<IPortableDeviceContent>       pContent;
CComPtr<IPortableDeviceValues>        pObjectPropertiesToWrite;
CComPtr<IPortableDeviceValues>        pPropertyWriteResults;
CComPtr<IPortableDeviceValues>        pAttributes;
BOOL                                  bCanWrite            = FALSE;
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");
}

After this, the application retrieves the WPD_PROPERTY_CAN_WRITE value for the WPD_OBJECT_NAME property to determine whether the property can be written. (Note that your application could set any property for which the WPD_PROPERTY_CAN_WRITE value is true.)

   HRESULT                               hr                   = S_OK;
   WCHAR                                 wszSelection[81]     = {0};
   WCHAR                                 wszNewObjectName[81] = {0};
   CComPtr<IPortableDeviceProperties>    pProperties;
   CComPtr<IPortableDeviceContent>       pContent;
   CComPtr<IPortableDeviceValues>        pObjectPropertiesToWrite;
   CComPtr<IPortableDeviceValues>        pPropertyWriteResults;
   CComPtr<IPortableDeviceValues>        pAttributes;
   BOOL                                  bCanWrite            = FALSE;
   if (SUCCEEDED(hr))
   {
       hr = pDevice->Content(&pContent;);
       if (FAILED(hr))
       {
           printf("! Failed to get IPortableDeviceContent from IPortableDevice, hr = 0x%lx\n",hr);
       }
   }



   if (SUCCEEDED(hr))
   {
       hr = pContent->Properties(&pProperties;);
       if (FAILED(hr))
       {
           printf("! Failed to get IPortableDeviceProperties from IPortableDevice, hr = 0x%lx\n",hr);
       }
   }



   if (SUCCEEDED(hr))
   {
       hr = pProperties->GetPropertyAttributes(wszSelection,
                                               WPD_OBJECT_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 the WPD_OBJECT_NAME reports TRUE\nThis means that the property can be changed/updated\n\n");
               }
               else
               {
                   printf("The attribute WPD_PROPERTY_ATTRIBUTE_CAN_WRITE for the WPD_OBJECT_NAME reports FALSE\nThis means that the property can be changed/updated\n\n");
               }
           }
           else
           {
               printf("! Failed to get the WPD_PROPERTY_ATTRIBUTE_CAN_WRITE value from WPD_OBJECT_NAME on object '%ws', hr = 0x%lx\n",wszSelection, hr);
           }
       }
   }

The next step is to prompt the user for the new name string. (Note that the call to the IPortableDeviceValues::SetStringValue method merely creates the property bag; the actual property has not been written yet.)

   HRESULT                               hr                   = S_OK;
   WCHAR                                 wszSelection[81]     = {0};
   WCHAR                                 wszNewObjectName[81] = {0};
   CComPtr<IPortableDeviceProperties>    pProperties;
   CComPtr<IPortableDeviceContent>       pContent;
   CComPtr<IPortableDeviceValues>        pObjectPropertiesToWrite;
   CComPtr<IPortableDeviceValues>        pPropertyWriteResults;
   CComPtr<IPortableDeviceValues>        pAttributes;
   BOOL                                  bCanWrite            = FALSE;
   if (bCanWrite)
   {
       printf("Enter the new WPD_OBJECT_NAME for the object '%ws'.\n>",wszSelection);
       hr = StringCbGetsW(wszNewObjectName,sizeof(wszNewObjectName));
       if (FAILED(hr))
       {
           printf("An invalid object name was specified, aborting property writing\n");
       }



       if (SUCCEEDED(hr))
       {
           hr = CoCreateInstance(CLSID_PortableDeviceValues,
                                 NULL,
                                 CLSCTX_INPROC_SERVER,
                                 IID_IPortableDeviceValues,
                                 (VOID**) &pObjectPropertiesToWrite;);
           if (SUCCEEDED(hr))
           {
               if (pObjectPropertiesToWrite != NULL)
               {
                   hr = pObjectPropertiesToWrite->SetStringValue(WPD_OBJECT_NAME, wszNewObjectName);
                   if (FAILED(hr))
                   {
                       printf("! Failed to add WPD_OBJECT_NAME to IPortableDeviceValues, hr= 0x%lx\n", hr);
                   }
               }
           }
       }

Finally, the new value, specified by the user, is applied to the object.

HRESULT                               hr                   = S_OK;
WCHAR                                 wszSelection[81]     = {0};
WCHAR                                 wszNewObjectName[81] = {0};
CComPtr<IPortableDeviceProperties>    pProperties;
CComPtr<IPortableDeviceContent>       pContent;
CComPtr<IPortableDeviceValues>        pObjectPropertiesToWrite;
CComPtr<IPortableDeviceValues>        pPropertyWriteResults;
CComPtr<IPortableDeviceValues>        pAttributes;
BOOL                                  bCanWrite            = FALSE;
    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 WPD_OBJECT_NAME property on object '%ws' was written successfully (Read the properties again to see the updated value)\n", wszSelection);
        }
    }

See Also

Previous Next