Udostępnij przez


Wyliczanie zawartości

Zawartość na urządzeniu (niezależnie od tego, czy ta zawartość jest folderem, książką telefoniczną, wideo, czy obrazem statycznym) nazywana jest obiektem w interfejsie API WPD. Te obiekty są identyfikowane za pomocą identyfikatorów obiektów i opisywane przez właściwości. Obiekty można wyliczyć na urządzeniu, wywołując metody w interfejsie IPortableDevice, interfejs IPortableDeviceContenti interfejs IEnumPortableDeviceObjectIDs.

Przykładowa aplikacja demonstruje wyliczenie zawartości w funkcji EnumerateAllContent znalezionej w module ContentEnumeration.cpp. Ta funkcja z kolei wywołuje funkcję RecursiveEnumerate, która przeprowadzi hierarchię obiektów znalezionych na wybranym urządzeniu i zwraca identyfikator obiektu dla każdego obiektu.

Jak wspomniano, funkcja RecursiveEnumerate pobiera identyfikator obiektu dla każdego obiektu znalezionego na urządzeniu. Identyfikator obiektu jest wartością ciągu. Po pobraniu tego identyfikatora aplikacja może uzyskać bardziej opisowe informacje o obiekcie (takie jak nazwa obiektu, identyfikator obiektu nadrzędnego itd.). Te informacje opisowe są określane jako właściwości obiektu (lub metadane). Aplikacja może pobrać te właściwości, wywołując metody interfejsu IPortableDeviceProperties.

Funkcja EnumerateAllContent zaczyna się od uzyskania wskaźnika do interfejsu IPortableDeviceContent . Pobiera ten wskaźnik, wywołując metodę 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);
    }
}

Po pobraniu wskaźnika do interfejsu IPortableDeviceContent funkcja EnumerateAllContent wywołuje funkcję RecursiveEnumerate, która przeprowadzi hierarchię obiektów znalezionych na danym urządzeniu i zwraca identyfikator obiektu dla każdego z nich.

Funkcja RecursiveEnumerate rozpoczyna się od pobierania wskaźnika do interfejsu IEnumPortableDeviceObjectIDs . Ten interfejs uwidacznia metody używane przez aplikację do nawigowania po liście obiektów znalezionych na danym urządzeniu.

W tym przykładzie funkcja RecursiveEnumerate wywołuje metodę IEnumPortableDeviceObjectIDs::Next, aby przejść przez listę obiektów.

Każde wywołanie metody IEnumPortableDeviceObjects::Next metoda żąda partii 10 identyfikatorów. (Ta wartość jest określana przez stałą NUM_OBJECTS_TO_REQUEST podaną jako pierwszy argument).

#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;
            }
        }
    }
}

interfejs IEnumPortableDeviceObjectIDs

interfejs IPortableDevice

IPortableDeviceContent Interface

Interfejs IPortableDeviceProperties

Przewodnik programowania