擷取單一對象的屬性
在您的應用程式擷取指定對象的物件標識碼(請參閱 列舉內容 主題)之後,它可以藉由呼叫 IPortableDeviceProperties 介面 和 IPortableDeviceKeyCollection 介面,擷取該物件的描述性資訊。
IPortableDeviceProperties::GetValues 方法會擷取指定物件的指定屬性清單。 (您的應用程式也可以呼叫 GetValues 方法,並指定 pKeys 參數的 NULL 值來擷取指定物件的所有屬性;不過,擷取所有屬性時,這個方法的效能可能會明顯變慢。
不過,在應用程式呼叫 GetValues 之前,它必須藉由在 IPortableDeviceKeyCollection 物件中設定對應的索引鍵來識別要擷取的屬性,。 您的應用程式會透過呼叫 IPortableDeviceKeyCollection::Add 方法,提供 PROPERTYKEY 值,以識別並擷取每個所需的屬性。
範例應用程式ContentProperties.cpp模組中的 ReadContentProperties 函式示範如何擷取所選物件的五個屬性。 下表說明這些屬性及其對應的 REFPROPERTYKEY 值。
財產 | 描述 | PROPERTYKEY |
---|---|---|
父物件標識碼 | 指定指定物件父系識別子的字串。 | WPD_OBJECT_PARENT_ID |
物件名稱 | 指定指定物件名稱的字串。 | WPD_OBJECT_NAME |
持續性唯一標識碼 | 用於指定給定物件唯一識別碼的字串。 (此標識符在會話之間是持續性的,與對象標識碼不同。 | WPD_OBJECT_PERSISTENT_UNIQUE_ID |
物件格式 | 全域唯一識別碼 (GUID),指定對應至指定物件之檔案的格式。 | WPD_物件格式 |
物件內容類型 | GUID,指定與指定對象相關聯的內容類型。 | WPD_OBJECT_CONTENT_TYPE |
ReadContentProperties 函式的下列摘錄示範範例應用程式如何使用 IPortableDeviceKeyCollection 介面 和 IPortableDeviceKeyCollection::Add 方法來識別其擷取的屬性。
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(WPD_OBJECT_PARENT_ID);
if (FAILED(hrTemp))
{
printf("! Failed to add WPD_OBJECT_PARENT_ID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
}
hrTemp = pPropertiesToRead->Add(WPD_OBJECT_NAME);
if (FAILED(hrTemp))
{
printf("! Failed to add WPD_OBJECT_NAME to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
}
hrTemp = pPropertiesToRead->Add(WPD_OBJECT_PERSISTENT_UNIQUE_ID);
if (FAILED(hrTemp))
{
printf("! Failed to add WPD_OBJECT_PERSISTENT_UNIQUE_ID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
}
hrTemp = pPropertiesToRead->Add(WPD_OBJECT_FORMAT);
if (FAILED(hrTemp))
{
printf("! Failed to add WPD_OBJECT_FORMAT to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
}
hrTemp = pPropertiesToRead->Add(WPD_OBJECT_CONTENT_TYPE);
if (FAILED(hrTemp))
{
printf("! Failed to add WPD_OBJECT_CONTENT_TYPE to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
}
}
}
一旦範例應用程式設定適當的索引鍵,它會呼叫 IPortableDeviceProperties::GetValues 方法來擷取指定物件的指定值。
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);
}
}
相關主題