擷取 WPD 物件屬性
服務通常包含屬於每個服務所支援其中一種格式的子物件。 例如,連絡人服務可支援多個抽象連絡人格式的連絡人物件。 每個連絡人物件都會依 (連絡人名稱、電話號碼、電子郵件地址等) 的相關屬性來描述。
WpdServiceApiSample 應用程式包含程式碼,示範應用程式如何擷取指定連絡人服務所支援的內容物件屬性。 此範例會使用下列介面。
介面 | 描述 |
---|---|
IPortableDeviceService | 擷取 IPortableDeviceContent2 介面,以存取支援的服務方法。 |
IPortableDeviceContent2 | 提供內容特定方法的存取權。 |
IPortableDeviceProperties | 擷取物件屬性值。 |
IPortableDeviceValues | 保留為該物件讀取的屬性值。 |
IPortableDeviceKeyCollection | 包含指定方法的參數。 |
當使用者在命令列選擇選項 「7」 時,應用程式會叫用 ContentProperties.cpp 模組中找到的 ReadContentProperties 方法。
這個方法會擷取指定之連絡人物件的下列四個屬性。
屬性 | 描述 | 裝置服務 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 |
- 父識別碼
- Name
- 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);
}
}
相關主題