寫入物件屬性
服務通常包含屬於每個服務所支援其中一種格式的子物件。 例如,連絡人服務可支援多個抽象連絡人格式的連絡人物件。 每個連絡人物件都會依 (連絡人名稱、電話號碼、電子郵件地址等) 的相關屬性來描述。
WpdServicesApiSample 應用程式包含程式碼,示範應用程式如何更新指定 Contacts 服務之子物件的名稱屬性。 此範例會使用下列 WPD 介面。
介面 | 描述 |
---|---|
IPortableDeviceService | 用來擷取 IPortableDeviceContent2 介面來存取支援的服務方法。 |
IPortableDeviceContent2 | 提供內容特定方法的存取權。 |
IPortableDeviceProperties | 用來寫入物件屬性值,以及判斷是否可以寫入指定的屬性 |
IPortableDeviceValues | 用來保存要寫入的屬性值、判斷寫入作業的結果,以及在判斷寫入功能) 時擷取屬性的屬性 (。 |
當使用者在命令列選擇選項 「8」 時,應用程式會叫用 ContentProperties.cpp 模組中找到的 WriteContentProperties 方法。 這個方法會提示使用者輸入要更新屬性的物件識別碼。 使用者會識別 物件,而 方法會提示使用者指定新名稱。 指定這個名稱之後,方法會更新指定物件的 Name 屬性。
請注意,在撰寫物件屬性之前,範例應用程式會在連線的裝置上開啟連絡人服務。
WriteContentProperties方法的下列程式碼示範應用程式如何使用IPortableDeviceContent2介面來擷取IPortableDeviceProperties介面。 將所要求屬性的 PROPERTYKEYS 傳遞至 IPortableDeviceProperties::SetValues 方法, WriteContentProperties 會更新 name 屬性。
void WriteContentProperties(
IPortableDeviceService* pService)
{
if (pService == NULL)
{
printf("! A NULL IPortableDeviceService interface pointer was received\n");
return;
}
HRESULT hr = S_OK;
WCHAR wszSelection[81] = {0};
WCHAR wszNewObjectName[81] = {0};
CComPtr<IPortableDeviceProperties> pProperties;
CComPtr<IPortableDeviceContent2> 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(wszSelection,sizeof(wszSelection));
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) Check the property attributes to see if we can write/change the NAME_GenericObj_Name property.
if (SUCCEEDED(hr))
{
hr = pProperties->GetPropertyAttributes(wszSelection,
PKEY_GenericObj_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 PKEY_GenericObj_Name reports TRUE\nThis means that the property can be changed/updated\n\n");
}
else
{
printf("The attribute WPD_PROPERTY_ATTRIBUTE_CAN_WRITE for PKEY_GenericObj_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 for PKEY_GenericObj_Name on object '%ws', hr = 0x%lx\n", wszSelection, hr);
}
}
}
// 4) Prompt the user for the new value of the NAME_GenericObj_Name property only if the property attributes report
// that it can be changed/updated.
if (bCanWrite)
{
printf("Enter the new PKEY_GenericObj_Name property for the object '%ws'.\n>",wszSelection);
hr = StringCbGetsW(wszNewObjectName,sizeof(wszNewObjectName));
if (FAILED(hr))
{
printf("An invalid PKEY_GenericObj_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_IPortableDeviceValues,
(VOID**) &pObjectPropertiesToWrite);
if (SUCCEEDED(hr))
{
if (pObjectPropertiesToWrite != NULL)
{
hr = pObjectPropertiesToWrite->SetStringValue(PKEY_GenericObj_Name, wszNewObjectName);
if (FAILED(hr))
{
printf("! Failed to add PKEY_GenericObj_Name to IPortableDeviceValues, hr= 0x%lx\n", hr);
}
}
}
}
// 6) Call SetValues() passing the collection of specified PROPERTYKEYs.
if (SUCCEEDED(hr))
{
hr = pProperties->SetValues(wszSelection, // 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", wszSelection, hr);
}
else
{
printf("The PKEY_GenericObj_Name property on object '%ws' was written successfully (Read the properties again to see the updated value)\n", wszSelection);
}
}
}
}
相關主題