Bagikan melalui


Mengambil properti objek WPD

Layanan sering berisi objek anak yang termasuk dalam salah satu format yang didukung setiap layanan. Misalnya, layanan Kontak dapat mendukung beberapa objek kontak dari format Kontak Abstrak. Setiap objek kontak dijelaskan oleh properti terkait (nama kontak, nomor telepon, alamat email, dan sebagainya).

Aplikasi WpdServiceApiSample menyertakan kode yang menunjukkan bagaimana aplikasi dapat mengambil properti objek konten yang didukung oleh layanan Kontak tertentu. Sampel ini menggunakan antarmuka berikut.

Antarmuka Deskripsi
IPortableDeviceService Mengambil antarmuka IPortableDeviceContent2 untuk mengakses metode layanan yang didukung.
IPortableDeviceContent2 Menyediakan akses ke metode khusus konten.
IPortableDeviceProperties Mengambil nilai properti objek.
IPortableDeviceValues Menyimpan nilai properti yang dibaca untuk objek tersebut.
IPortableDeviceKeyCollection Berisi parameter untuk metode tertentu.

 

Ketika pengguna memilih opsi "7" di baris perintah, aplikasi memanggil metode ReadContentProperties yang ditemukan dalam modul ContentProperties.cpp.

Metode ini mengambil empat properti berikut untuk objek kontak yang ditentukan.

Properti Deskripsi PROPERTYKEY Layanan Perangkat WPD_PROPERTYKEY setara
Pengidentifikasi objek induk String yang menentukan pengidentifikasi untuk induk objek yang diberikan. PKEY_GenericObj_ParentID WPD_OBJECT_PARENT_ID
Nama Objek String yang menentukan nama objek yang diberikan PKEY_GenericObj_Name 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. Untuk layanan, ini harus menjadi representasi string dari GUID. PKEY_GenericObj_PersistentUID WPD_OBJECT_PERSISTENT_UNIQUE_ID
Format objek Pengidentifikasi unik global (GUID) yang menentukan format file yang sesuai dengan objek tertentu PKEY_GenericObj_ObjectFormat WPD_OBJECT_FORMAT

 

  • ID Induk
  • Nama
  • PersistentUID
  • Format

Perhatikan bahwa sebelum mengambil properti konten, aplikasi sampel membuka layanan Kontak pada perangkat yang tersambung.

Kode berikut untuk metode ReadContentProperties menunjukkan bagaimana aplikasi menggunakan antarmuka IPortableDeviceContent2 untuk mengambil antarmuka IPortableDeviceProperties . Dengan meneruskan PROPERTYKEYS dari properti yang diminta ke metode IPortableDeviceProperties::GetValues , ReadContentProperties mengambil nilai yang diminta, lalu menampilkannya ke jendela konsol.

// Reads properties for the user specified object.
void ReadContentProperties(
    IPortableDeviceService*    pService)
{
    if (pService == NULL)
    {
        printf("! A NULL IPortableDeviceService interface pointer was received\n");
        return;
    }

    HRESULT                               hr              = S_OK;
    WCHAR                                 szSelection[81] = {0};
    CComPtr<IPortableDeviceProperties>    pProperties;
    CComPtr<IPortableDeviceValues>        pObjectProperties;
    CComPtr<IPortableDeviceContent2>      pContent;
    CComPtr<IPortableDeviceKeyCollection> pPropertiesToRead;

    // Prompt user to enter an object identifier on the device to read properties from.
    printf("Enter the identifer of the object you wish to read properties from.\n>");
    hr = StringCbGetsW(szSelection,sizeof(szSelection));
    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) CoCreate an IPortableDeviceKeyCollection interface to hold the property keys
    // we wish to read.
    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(PKEY_GenericObj_ParentID);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add PKEY_GenericObj_ParentID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

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

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

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

        }
    }

    // 5) Call GetValues() passing the collection of specified PROPERTYKEYs.
    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);
        }
    }

    // 6) Display the returned property values to the user
    if (SUCCEEDED(hr))
    {
        DisplayStringProperty(pObjectProperties, PKEY_GenericObj_ParentID,        NAME_GenericObj_ParentID);
        DisplayStringProperty(pObjectProperties, PKEY_GenericObj_Name,            NAME_GenericObj_Name);
        DisplayStringProperty(pObjectProperties, PKEY_GenericObj_PersistentUID,   NAME_GenericObj_PersistentUID);
        DisplayGuidProperty  (pObjectProperties, PKEY_GenericObj_ObjectFormat,    NAME_GenericObj_ObjectFormat);
    }
}

IPortableDeviceContent2

IPortableDeviceProperties

WpdServicesApiSample