列舉內容
裝置上的內容 (該內容是資料夾、電話簿、視訊或仍然影像) 稱為 WPD API 中的物件。 這些物件是由物件識別碼所參考,並由屬性所描述。 您可以呼叫 IPortableDevice 介面、 IPortableDeviceContent 介面和 IEnumPortableDeviceObjectIDs 介面中的方法,列舉裝置上的物件。
範例應用程式會在 ContentEnumeration.cpp 模組中找到的 EnumerateAllContent 函式中示範內容列舉。 接著,此函式會呼叫 RecursiveEnumerate 函式,此函式會逐步解說所選裝置上找到的物件階層,並傳回每個物件的物件識別碼。
如前所述,RecursiveEnumerate 函式會針對裝置上找到的每個物件擷取物件識別碼。 物件識別碼是字串值。 一旦應用程式擷取此識別碼,就可以取得更具描述性的物件資訊 (,例如物件的名稱、物件的父系識別碼,依此類) 。 這個描述性資訊稱為物件屬性 (或中繼資料) 。 您的應用程式可以藉由呼叫 IPortableDeviceProperties 介面的成員來擷取這些屬性。
EnumerateAllContent 函式會從擷取 IPortableDeviceContent 介面的指標開始。 它會藉由呼叫 IPortableDevice::Content 方法來擷取此指標。
void EnumerateAllContent(
IPortableDevice* pDevice)
{
HRESULT hr = S_OK;
CComPtr<IPortableDeviceContent> pContent;
if (pDevice == NULL)
{
printf("! A NULL IPortableDevice interface pointer was received\n");
return;
}
// Get an IPortableDeviceContent interface from the IPortableDevice interface to
// access the content-specific methods.
hr = pDevice->Content(&pContent);
if (FAILED(hr))
{
printf("! Failed to get IPortableDeviceContent from IPortableDevice, hr = 0x%lx\n",hr);
}
// Enumerate content starting from the "DEVICE" object.
if (SUCCEEDED(hr))
{
printf("\n");
RecursiveEnumerate(WPD_DEVICE_OBJECT_ID, pContent);
}
}
一旦擷取 IPortableDeviceContent 介面的指標,EnumerateAllContent 函式會呼叫 RecursiveEnumerate 函式,以逐步執行指定裝置上找到的物件階層,並傳回每個物件識別碼。
RecursiveEnumerate 函式會從擷取 IEnumPortableDeviceObjectIDs 介面的指標開始。 這個介面會公開應用程式用來巡覽指定裝置上找到之物件清單的方法。
在此範例中,RecursiveEnumerate 函式會呼叫 IEnumPortableDeviceObjectIDs::Next 方法來周遊物件清單。
每次呼叫 IEnumPortableDeviceObjects::Next 方法都會要求 10 個識別碼的批次。 (這個值是由提供做為第一個引數的NUM_OBJECTS_TO_REQUEST常數所指定。)
#define NUM_OBJECTS_TO_REQUEST 10
// Recursively called function which enumerates using the specified
// object identifier as the parent.
void RecursiveEnumerate(
PCWSTR pszObjectID,
IPortableDeviceContent* pContent)
{
CComPtr<IEnumPortableDeviceObjectIDs> pEnumObjectIDs;
// Print the object identifier being used as the parent during enumeration.
printf("%ws\n",pszObjectID);
// Get an IEnumPortableDeviceObjectIDs interface by calling EnumObjects with the
// specified parent object identifier.
HRESULT hr = pContent->EnumObjects(0, // Flags are unused
pszObjectID, // Starting from the passed in object
NULL, // Filter is unused
&pEnumObjectIDs);
if (FAILED(hr))
{
printf("! Failed to get IEnumPortableDeviceObjectIDs from IPortableDeviceContent, hr = 0x%lx\n",hr);
}
// Loop calling Next() while S_OK is being returned.
while(hr == S_OK)
{
DWORD cFetched = 0;
PWSTR szObjectIDArray[NUM_OBJECTS_TO_REQUEST] = {0};
hr = pEnumObjectIDs->Next(NUM_OBJECTS_TO_REQUEST, // Number of objects to request on each NEXT call
szObjectIDArray, // Array of PWSTR array which will be populated on each NEXT call
&cFetched); // Number of objects written to the PWSTR array
if (SUCCEEDED(hr))
{
// Traverse the results of the Next() operation and recursively enumerate
// Remember to free all returned object identifiers using CoTaskMemFree()
for (DWORD dwIndex = 0; dwIndex < cFetched; dwIndex++)
{
RecursiveEnumerate(szObjectIDArray[dwIndex],pContent);
// Free allocated PWSTRs after the recursive enumeration call has completed.
CoTaskMemFree(szObjectIDArray[dwIndex]);
szObjectIDArray[dwIndex] = NULL;
}
}
}
}
相關主題