共用方式為


列舉內容

裝置上的內容(該內容是資料夾、電話簿、視訊或靜止影像)在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;
            }
        }
    }
}

IEnumPortableDeviceObjectIDs 介面

IPortableDevice 介面

IPortableDeviceContent 介面

IPortableDeviceProperties 介面

程式設計手冊