将Properties-Only对象传输到设备

虽然上一主题中的示例演示了在包含属性和数据的设备上创建内容,但本主题重点介绍如何创建仅属性对象。

仅属性传输是使用下表中所述的接口完成的。

接口 说明
IPortableDeviceContent 接口 提供对特定于内容的方法的访问。
IPortableDeviceValues 接口 用于检索描述内容的属性。

 

TransferContactToDevice示例应用程序的 ContentTransfer.cpp 模块中的函数演示了应用程序如何将联系人信息从电脑传输到连接的设备。 在此特定示例中,转移的联系人姓名是硬编码的“John Kane”,传输的联系人电话号码始终为“425-555-0123”。

函数完成 TransferContactToDevice 的第一个任务是提示用户在设备上输入父对象的对象标识符, (将在设备上传输内容) 。

HRESULT                             hr = S_OK;
WCHAR                               szSelection[81]        = {0};
CComPtr<IPortableDeviceValues>      pFinalObjectProperties;
CComPtr<IPortableDeviceContent>     pContent;

// Prompt user to enter an object identifier for the parent object on the device to transfer.
printf("Enter the identifer of the parent object which the contact will be transferred under.\n>");
hr = StringCbGetsW(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
    printf("An invalid object identifier was specified, aborting content transfer\n");
}

下一步是检索联系人属性,当示例将新联系人写入设备时,将使用该属性。 联系人属性的检索由 GetRequiredPropertiesForPropertiesOnlyContact 帮助程序函数执行。

// 2) Get the properties that describe the object being created on the device

if (SUCCEEDED(hr))
{
    hr = GetRequiredPropertiesForPropertiesOnlyContact(szSelection,              // Parent to transfer the data under
                                                       &pFinalObjectProperties);  // Returned properties describing the data
    if (FAILED(hr))
    {
        printf("! Failed to get required properties needed to transfer an image file to the device, hr = 0x%lx\n", hr);
    }
}

此函数将以下数据写入 IPortableDeviceValues 对象。

  • 设备上联系人的父级的对象标识符。 (这是设备将存储对象的目标。)
  • 对象类型 (联系人) 。
  • 联系人姓名 (硬编码字符串“John Kane”) 。
  • 联系人电话号码 (硬编码字符串“425-555-0123”) 。

最后一步使用帮助程序函数 () 设置 GetRequiredPropertiesForPropertiesOnlyContact 的属性在设备上创建仅属性对象。 这是通过调用 IPortableDeviceContent::CreateObjectWithPropertiesOnly 方法实现的。

if (SUCCEEDED(hr))
{
    PWSTR pszNewlyCreatedObject = NULL;
    hr = pContent->CreateObjectWithPropertiesOnly(pFinalObjectProperties,    // Properties describing the object data
                                                  &pszNewlyCreatedObject);
    if (SUCCEEDED(hr))
    {
        printf("The contact was transferred to the device.\nThe newly created object's ID is '%ws'\n",pszNewlyCreatedObject);
    }

    if (FAILED(hr))
    {
        printf("! Failed to transfer contact object to the device, hr = 0x%lx\n",hr);
    }

    // Free the object identifier string returned from CreateObjectWithPropertiesOnly
    CoTaskMemFree(pszNewlyCreatedObject);
    pszNewlyCreatedObject = NULL;
}

IPortableDevice 接口

IPortableDeviceContent 接口

IPortableDeviceValues 接口

编程指南