擷取 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 |
- 父標識碼
- 名字
- 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);
}
}
相關主題