共用方式為


IEnumPortableDeviceObjectIDs::Next 方法 (portabledeviceapi.h)

Next 方法會擷取列舉序列中的下一個或多個物件標識碼。

語法

HRESULT Next(
  [in]      ULONG  cObjects,
  [in, out] LPWSTR *pObjIDs,
  [in, out] ULONG  *pcFetched
);

參數

[in] cObjects

要求的物件計數。

[in, out] pObjIDs

LPWSTR 指標的陣列,每個指標都會指定擷取的物件標識碼。 呼叫端必須配置 cObjects LPWSTR 元素的陣列。 呼叫端必須釋放數位和傳回的字串。 字串可藉由呼叫 CoTaskMemFree 來釋出。

[in, out] pcFetched

在輸入時,會忽略此參數。 在輸出上,實際擷取的標識碼數目。 如果沒有釋放物件標識碼,且傳回值S_FALSE,則不會再列舉任何物件。

傳回值

方法會傳回 HRESULT。 可能的值包括 (但不限於) 下表中的這些值。

傳回碼 描述
S_OK
此方法已成功。
S_FALSE
沒有其他要列舉的物件。

備註

如果少於要求的元素數目保留在序列中,這個方法會擷取其餘元素。 實際擷取的項目數目會透過 pcFetched (傳回,除非呼叫端為該參數傳入NULL) 。 列舉物件全都是對等,也就是說,列舉物件的子系只會列舉直接子系,而不是子系或更深入的物件。

範例


// This number controls how many object identifiers are requested during each call
// to IEnumPortableDeviceObjectIDs::Next()
#define NUM_OBJECTS_TO_REQUEST  10

// Recursively called function which enumerates using the specified
// object identifier as the parent.
void RecursiveEnumerate(LPCWSTR wszParentObjectID, IPortableDeviceContent* pContent)
{
    HRESULT                       hr             = S_OK;
    IEnumPortableDeviceObjectIDs* pEnumObjectIDs = NULL;

    if ((wszParentObjectID == NULL) ||
        (pContent          == NULL))
    {
        return;
    }

    // wszParentObjectID is the object identifier of the parent being used for enumeration

    // Get an IEnumPortableDeviceObjectIDs interface by calling EnumObjects with the
    // specified parent object identifier.
    hr = pContent->EnumObjects(0, wszParentObjectID, NULL, &pEnumObjectIDs);
    if (FAILED(hr))
    {
        // Failed to get IEnumPortableDeviceObjectIDs from IPortableDeviceContent
    }

    // Loop calling Next() while S_OK is being returned.
    while(hr == S_OK)
    {
        DWORD  cFetched = 0;
        LPWSTR 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 LPWSTR array which will be populated on each NEXT call
                                  &cFetched);             // Number of objects written to the LPWSTR 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 LPWSTRs after the recursive enumeration call has completed.
                CoTaskMemFree(szObjectIDArray[dwIndex]);
                szObjectIDArray[dwIndex] = NULL;
            }
        }
    }

    // Release the IEnumPortableDeviceObjectIDs when finished
    if (pEnumObjectIDs != NULL)
    {
        pEnumObjectIDs->Release();
        pEnumObjectIDs = NULL;
    }
}

規格需求

需求
目標平台 Windows
標頭 portabledeviceapi.h
程式庫 PortableDeviceGUIDs.lib

另請參閱

列舉內容

IEnumPortableDeviceObjectIDs 介面