Bagikan melalui


Mengambil Properti untuk Objek Tunggal

Setelah aplikasi Anda mengambil pengidentifikasi objek (lihat topik Enumerasi Konten ) untuk objek tertentu, aplikasi dapat mengambil informasi deskriptif tentang objek tersebut dengan memanggil metode di antarmuka IPortableDeviceProperties dan antarmuka IPortableDeviceKeyCollection.

Metode IPortableDeviceProperties::GetValues mengambil daftar properti tertentu untuk objek tertentu. (Aplikasi Anda juga dapat memanggil metode GetValues dan menentukan nilai NULL untuk parameter pKeys untuk mengambil semua properti untuk objek tertentu; namun, performa metode ini mungkin jauh lebih lambat saat mengambil semua properti.)

Namun, sebelum aplikasi Anda memanggil GetValues, aplikasi perlu mengidentifikasi properti yang akan diambil dengan mengatur kunci yang sesuai dalam objek IPortableDeviceKeyCollection. Aplikasi Anda akan mengidentifikasi properti yang menarik dengan memanggil metode IPortableDeviceKeyCollection::Add dan menyediakan nilai PROPERTYKEY yang mengidentifikasi setiap properti yang akan diambilnya.

Fungsi ReadContentProperties dalam modul ContentProperties.cpp dari aplikasi sampel menunjukkan bagaimana lima properti diambil untuk objek yang dipilih. Tabel berikut ini menjelaskan masing-masing properti ini dan nilai REFPROPERTYKEY terkait.

Properti Deskripsi PROPERTYKEY
Pengidentifikasi objek induk String yang menentukan pengidentifikasi untuk induk objek yang diberikan. WPD_OBJECT_PARENT_ID
Nama Objek String yang menentukan nama objek yang diberikan. WPD_OBJECT_NAME
Pengidentifikasi unik persisten String yang menentukan pengidentifikasi unik untuk objek yang diberikan. (Pengidentifikasi ini persisten di seluruh sesi, tidak seperti pengidentifikasi objek.) WPD_OBJECT_PERSISTENT_UNIQUE_ID
Format objek Pengidentifikasi unik global (GUID) yang menentukan format file yang sesuai dengan objek tertentu. WPD_OBJECT_FORMAT
Tipe konten objek GUID yang menentukan jenis konten yang terkait dengan objek tertentu. WPD_OBJECT_CONTENT_TYPE

 

Kutipan berikut dari fungsi ReadContentProperties menunjukkan bagaimana aplikasi sampel menggunakan antarmuka IPortableDeviceKeyCollection dan IPortableDeviceKeyCollection::Tambahkan metode untuk mengidentifikasi properti yang akan diambilnya.

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(WPD_OBJECT_PARENT_ID);
        if (FAILED(hrTemp))
        {
            printf("! Failed to add WPD_OBJECT_PARENT_ID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
        }

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

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

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

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

Setelah aplikasi sampel mengatur kunci yang sesuai, aplikasi tersebut memanggil metode IPortableDeviceProperties::GetValues untuk mengambil nilai yang ditentukan untuk objek yang diberikan.

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

Antarmuka IPortableDevice

Antarmuka IPortableDeviceKeyCollection

Antarmuka IPortableDeviceProperties

Panduan Pemrograman