Mengatur Properti untuk Objek Tunggal

Setelah aplikasi Anda mengambil pengidentifikasi objek (lihat topik Menghitung Konten ) untuk objek tertentu, aplikasi dapat mengatur properti untuk objek tersebut dengan memanggil metode di antarmuka yang dijelaskan dalam tabel berikut.

Antarmuka Deskripsi
Antarmuka IPortableDeviceProperties Digunakan untuk menentukan apakah properti tertentu dapat ditulis dan untuk mengirim operasi tulis.
Antarmuka IPortableDeviceContent Menyediakan akses ke metode khusus konten.
Antarmuka IPortableDeviceValues Digunakan untuk menahan nilai yang akan ditulis, menentukan hasil operasi tulis, dan mengambil atribut properti (saat menentukan kemampuan tulis).

 

Aplikasi mengatur properti pada objek dengan terlebih dahulu membuat tas properti yang menentukan nilai baru menggunakan antarmuka IPortableDeviceValues. Setelah tas properti dibuat, aplikasi menetapkan properti tersebut dengan memanggil metode IPortableDeviceProperties::SetValues .

Fungsi WriteContentProperties dalam modul ContentProperties.cpp aplikasi sampel menunjukkan bagaimana aplikasi dapat mengatur properti nama objek baru untuk objek yang dipilih.

Tugas pertama yang dilakukan oleh WriteContentProperties fungsi adalah meminta pengguna untuk memasukkan pengidentifikasi objek untuk objek tempat aplikasi akan menulis nama baru.

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");
}

Setelah ini, aplikasi mengambil nilai WPD_PROPERTY_ATTRIBUTE_CAN_WRITE untuk properti WPD_OBJECT_NAME untuk menentukan apakah properti dapat ditulis. (Perhatikan bahwa aplikasi Anda dapat mengatur properti apa pun yang nilai WPD_PROPERTY_ATTRIBUTE_CAN_WRITE nya benar.)

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);
        }
    }
}

Langkah selanjutnya adalah meminta string nama baru kepada pengguna. (Perhatikan bahwa panggilan ke metode IPortableDeviceValues::SetStringValue hanya membuat tas properti; properti aktual belum ditulis.)

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);
                }
            }
        }
    }

Terakhir, nilai baru, yang ditentukan oleh pengguna, diterapkan ke objek .

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);
    }
}

Antarmuka IPortableDevice

Antarmuka IPortableDeviceContent

Antarmuka IPortableDeviceKeyCollection

Antarmuka IPortableDeviceProperties

Antarmuka IPortableDevicePropertiesBulk

Antarmuka IPortableDevicePropVariantCollection

Panduan Pemrograman