Condividi tramite


Enumerazione del contenuto

Il contenuto in un dispositivo (se il contenuto è una cartella, una rubrica telefonica, un video o un'immagine ancora) viene chiamato un oggetto nell'API WPD. Questi oggetti vengono a cui fa riferimento gli identificatori di oggetto e descritti dalle proprietà. È possibile enumerare gli oggetti in un dispositivo chiamando i metodi nell'interfaccia IPortableDevice, l'interfaccia IPortableDeviceContent e l'interfaccia IEnumPortableDeviceObjectIDs.

L'applicazione di esempio illustra l'enumerazione del contenuto nella funzione EnumerateAllContent disponibile nel modulo ContentEnumeration.cpp. Questa funzione, a sua volta, chiama una funzione RecursiveEnumerate che illustra la gerarchia di oggetti trovati nel dispositivo selezionato e restituisce un identificatore di oggetto per ogni oggetto.

Come indicato, la funzione RecursiveEnumerate recupera un identificatore di oggetto per ogni oggetto trovato nel dispositivo. L'identificatore dell'oggetto è un valore stringa. Dopo aver recuperato questo identificatore, l'applicazione può ottenere informazioni più descrittive sull'oggetto, ad esempio il nome dell'oggetto, l'identificatore per l'elemento padre dell'oggetto e così via. Queste informazioni descrittive vengono definite proprietà dell'oggetto (o metadati). L'applicazione può recuperare queste proprietà chiamando i membri dell'interfaccia IPortableDeviceProperties.

La funzione EnumerateAllContent inizia recuperando un puntatore a un'interfaccia IPortableDeviceContent. Recupera questo puntatore chiamando il metodo 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);
    }
}

Dopo aver recuperato il puntatore all'interfaccia IPortableDeviceContent, la funzione EnumerateAllContent chiama la funzione RecursiveEnumerate, che illustra la gerarchia di oggetti trovati nel dispositivo specificato e restituisce un identificatore di oggetto per ogni oggetto.

La funzione RecursiveEnumerate inizia recuperando un puntatore a un'interfaccia IEnumPortableDeviceObjectIDs. Questa interfaccia espone i metodi usati da un'applicazione per esplorare l'elenco di oggetti trovati in un determinato dispositivo.

In questo esempio la funzione RecursiveEnumerate chiama il metodo IEnumPortableDeviceObjectIDs::Next per attraversare l'elenco di oggetti.

Ogni chiamata al metodo IEnumPortableDeviceObjects::Next richiede un batch di 10 identificatori. Questo valore viene specificato dalla costante NUM_OBJECTS_TO_REQUEST fornita come primo argomento.

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

Interfaccia IEnumPortableDeviceObjectIDs

Interfaccia IPortableDevice

Interfaccia IPortableDeviceContent

Interfaccia IPortableDeviceProperties

Guida alla programmazione