設定單一物件的屬性

當應用程式擷取物件識別碼 (查看指定物件的 列舉內容 主題) 之後,就可以在下表所述的介面中呼叫方法來設定該物件的屬性。

介面 描述
IPortableDeviceProperties 介面 用來判斷是否可以寫入指定的屬性,以及傳送寫入作業。
IPortableDeviceContent 介面 提供內容特定方法的存取權。
IPortableDeviceValues 介面 用來保存要寫入的值、判斷寫入作業的結果,以及在判斷寫入功能時擷取屬性的屬性) (。

 

應用程式會先建立屬性包,以使用 IPortableDeviceValues 介面來指定新值,以在物件上設定屬性。 建立屬性包之後,應用程式會呼叫 IPortableDeviceProperties::SetValues 方法來設定這些屬性。

範例 WriteContentProperties 應用程式 ContentProperties.cpp 模組中的函式示範應用程式如何為選取的物件設定新的物件名稱屬性。

函式完成 WriteContentProperties 的第一項工作是提示使用者輸入應用程式將寫入新名稱的物件物件識別碼。

HRESULT                               hr                   = S_OK;
WCHAR                                 szSelection[81]      = {0};
WCHAR                                 szNewObjectName[81]  = {0};
CComPtr<IPortableDeviceProperties>    pProperties;
CComPtr<IPortableDeviceContent>       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(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
    printf("An invalid object identifier was specified, aborting property reading\n");
}

之後,應用程式會擷取WPD_OBJECT_NAME屬性的WPD_PROPERTY_ATTRIBUTE_CAN_WRITE值,以判斷是否可以寫入屬性。 (請注意,您的應用程式可以設定WPD_PROPERTY_ATTRIBUTE_CAN_WRITE值為 true 的任何屬性。)

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

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

// 3) Check the property attributes to see if we can write/change the WPD_OBJECT_NAME property.
if (SUCCEEDED(hr))
{
    hr = pProperties->GetPropertyAttributes(szSelection,
                                            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 cannot 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",szSelection, hr);
        }
    }
}

下一個步驟是提示使用者輸入新的名稱字串。 (請注意,呼叫 IPortableDeviceValues::SetStringValue 方法只會建立屬性包;實際屬性尚未寫入。)

if (bCanWrite)
{
    printf("Enter the new WPD_OBJECT_NAME for the object '%ws'.\n>",szSelection);
    hr = StringCbGetsW(szNewObjectName,sizeof(szNewObjectName));
    if (FAILED(hr))
    {
        printf("An invalid object 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_PPV_ARGS(&pObjectPropertiesToWrite));
        if (SUCCEEDED(hr))
        {
            if (pObjectPropertiesToWrite != NULL)
            {
                hr = pObjectPropertiesToWrite->SetStringValue(WPD_OBJECT_NAME, szNewObjectName);
                if (FAILED(hr))
                {
                    printf("! Failed to add WPD_OBJECT_NAME to IPortableDeviceValues, hr= 0x%lx\n", hr);
                }
            }
        }
    }

最後,使用者指定的新值會套用至 物件。

if (SUCCEEDED(hr))
{
    hr = pProperties->SetValues(szSelection,                // 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", szSelection, hr);
    }
    else
    {
        printf("The WPD_OBJECT_NAME property on object '%ws' was written successfully (Read the properties again to see the updated value)\n", szSelection);
    }
}

IPortableDevice 介面

IPortableDeviceContent 介面

IPortableDeviceKeyCollection 介面

IPortableDeviceProperties 介面

IPortableDevicePropertiesBulk 介面

IPortableDevicePropVariantCollection 介面

程式設計指南