检索 WPD 对象属性

服务通常包含属于每个服务支持的格式之一的子对象。 例如,联系人服务可能支持抽象联系人格式的多个联系人对象。 每个联系人对象由相关属性 (联系人姓名、电话号码、电子邮件地址等) 描述。

WpdServiceApiSample 应用程序包含代码,演示如何应用程序检索给定联系人服务支持的内容对象属性。 此示例使用以下接口。

接口 说明
IPortableDeviceService 检索 IPortableDeviceContent2 接口以访问受支持的服务方法。
IPortableDeviceContent2 提供对特定于内容的方法的访问权限。
IPortableDeviceProperties 检索对象属性值。
IPortableDeviceValues 保存为该对象读取的属性值。
IPortableDeviceKeyCollection 包含给定方法的参数。

 

当用户在命令行中选择选项“7”时,应用程序将调用 ContentProperties.cpp 模块中找到的 ReadContentProperties 方法。

此方法检索指定联系人对象的以下四个属性。

属性 说明 Device Services PROPERTYKEY 等效WPD_PROPERTYKEY
父对象标识符 一个字符串,指定给定对象的父对象的标识符。 PKEY_GenericObj_ParentID WPD_OBJECT_PARENT_ID
对象名称 一个字符串,指定给定对象的名称 PKEY_GenericObj_Name WPD_OBJECT_NAME
持久唯一标识符 一个字符串,指定给定对象的唯一标识符。 此标识符在会话中持久存在,与对象标识符不同。 对于服务,这必须是 GUID 的字符串表示形式。 PKEY_GenericObj_PersistentUID WPD_OBJECT_PERSISTENT_UNIQUE_ID
对象格式 全局唯一标识符 (GUID) ,指定与给定对象对应的文件格式 PKEY_GenericObj_ObjectFormat WPD_OBJECT_FORMAT

 

  • 父 ID
  • 名称
  • PersistentUID
  • 格式

请注意,在检索内容属性之前,示例应用程序会在连接的设备上打开联系人服务。

ReadContentProperties 方法的以下代码演示了应用程序如何使用 IPortableDeviceContent2 接口检索 IPortableDeviceProperties 接口。 通过将请求属性的 PROPERTYKEYS 传递给 IPortableDeviceProperties::GetValues 方法, ReadContentProperties 将检索请求的值,然后将其显示在控制台窗口中。

// 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