设置单个对象的属性
在应用程序检索对象标识符 (查看给定对象的 枚举内容 主题) 后,它可以通过调用下表所述的接口中的方法来设置该对象的属性。
接口 | 说明 |
---|---|
IPortableDeviceProperties 接口 | 用于确定是否可以写入给定属性以及发送写入操作。 |
IPortableDeviceContent 接口 | 提供对特定于内容的方法的访问。 |
IPortableDeviceValues 接口 | 用于保存要写入的值、确定写入操作的结果,并在确定写入功能) 时检索属性 (的属性。 |
应用程序通过首先创建使用 IPortableDeviceValues 接口指定新值的属性包来设置对象的属性。 创建属性包后,应用程序通过调用 IPortableDeviceProperties::SetValues 方法设置这些属性。
WriteContentProperties
示例应用程序的 ContentProperties.cpp 模块中的 函数演示了应用程序如何为所选对象设置新的对象名称属性。
函数完成 WriteContentProperties
的第一个任务是提示用户输入应用程序将为其写入新名称的对象的对象标识符。
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");
}
之后,应用程序检索WPD_OBJECT_NAME属性的WPD_PROPERTY_ATTRIBUTE_CAN_WRITE值,以确定是否可以写入该属性。 (请注意,应用程序可以设置WPD_PROPERTY_ATTRIBUTE_CAN_WRITE值为 true 的任何属性。)
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);
}
}
}
下一步是提示用户输入新名称字符串。 (请注意,对 IPortableDeviceValues::SetStringValue 方法的调用仅创建属性包;实际属性尚未写入。)
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);
}
}
}
}
最后,由用户指定的新值将应用于 对象。
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);
}
}
相关主题