콘텐츠 열거
디바이스의 콘텐츠(해당 콘텐츠가 폴더, 전화 번호부, 비디오 또는 스틸 이미지인지 여부)를 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;
}
}
}
}
관련 항목
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기