Bagikan melalui


Menghitung Konten

Konten pada perangkat (baik konten tersebut adalah folder, buku telepon, video, atau gambar diam) disebut objek di API WPD. Objek ini dirujuk oleh pengidentifikasi objek dan dijelaskan oleh properti. Anda dapat menghitung objek pada perangkat dengan memanggil metode di antarmuka IPortableDevice, antarmuka IPortableDeviceContent, dan antarmuka IEnumPortableDeviceObjectIDs.

Aplikasi sampel menunjukkan enumerasi konten dalam fungsi EnumerateAllContent yang ditemukan dalam modul ContentEnumeration.cpp. Fungsi ini, pada gilirannya, memanggil fungsi RecursiveEnumerate yang berjalan hierarki objek yang ditemukan pada perangkat yang dipilih dan mengembalikan pengidentifikasi objek untuk setiap objek.

Seperti disebutkan, fungsi RecursiveEnumerate mengambil pengidentifikasi objek untuk setiap objek yang ditemukan pada perangkat. Pengidentifikasi objek adalah nilai string. Setelah aplikasi Anda mengambil pengidentifikasi ini, aplikasi dapat memperoleh informasi objek yang lebih deskriptif (seperti nama objek, pengidentifikasi untuk induk objek, dan sebagainya). Informasi deskriptif ini disebut sebagai properti objek (atau metadata). Aplikasi Anda dapat mengambil properti ini dengan memanggil anggota antarmuka IPortableDeviceProperties.

Fungsi EnumerateAllContent dimulai dengan mengambil penunjuk ke antarmuka IPortableDeviceContent. Ini mengambil pointer ini dengan memanggil metode 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);
    }
}

Setelah mengambil penunjuk ke antarmuka IPortableDeviceContent, fungsi EnumerateAllContent memanggil fungsi RecursiveEnumerate, yang berjalan hierarki objek yang ditemukan pada perangkat tertentu dan mengembalikan pengidentifikasi objek untuk masing-masing objek.

Fungsi RecursiveEnumerate dimulai dengan mengambil penunjuk ke antarmuka IEnumPortableDeviceObjectIDs. Antarmuka ini mengekspos metode yang digunakan aplikasi untuk menavigasi daftar objek yang ditemukan pada perangkat tertentu.

Dalam sampel ini, fungsi RecursiveEnumerate memanggil metode IEnumPortableDeviceObjectIDs::Next untuk melintasi daftar objek.

Setiap panggilan ke IEnumPortableDeviceObjects::Metode berikutnya meminta batch 10 pengidentifikasi. (Nilai ini ditentukan oleh konstanta NUM_OBJECTS_TO_REQUEST yang disediakan sebagai argumen pertama.)

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

Antarmuka IEnumPortableDeviceObjectIDs

Antarmuka IPortableDevice

Antarmuka IPortableDeviceContent

Antarmuka IPortableDeviceProperties

Panduan Pemrograman