IEnumPortableDeviceObjectIDs::Next 方法 (portabledeviceapi.h)

Next 方法检索枚举序列中接下来的一个或多个对象 ID。

语法

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

参数

[in] cObjects

请求的对象计数。

[in, out] pObjIDs

LPWSTR 指针的数组,每个指针指定检索到的对象 ID。 调用方必须分配 cObjects LPWSTR 元素的数组。 调用方必须释放数组和返回的字符串。 通过调用 CoTaskMemFree 释放字符串。

[in, out] pcFetched

输入时,将忽略此参数。 输出时,实际检索到的 ID 数。 如果未释放任何对象 ID 且返回值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
Library PortableDeviceGUIDs.lib

另请参阅

枚举内容

IEnumPortableDeviceObjectIDs 接口