[This is preliminary documentation and subject to change.]
The IEnumWbemClassObject::Next method returns the next object or objects starting at the current cursor position. It then advances the cursor position by that many objects, so that subsequent calls return the subsequent objects.
HRESULT Next(
[in] long lTimeOut,
[in] ULONGARG uCount,
[out, size_is(uCount), length_is(*puReturned)] IWbemClassObject **ppObjects,
[out] ULONGARG *puReturned
);
Note The IWbemClassObject::Next method returns S_FALSE even when a nonzero number of objects is returned. S_OK is returned only when the number of objects returned matches the number requested in uCount. Therefore, loop termination logic should examine the puReturned value to ensure you have reached the end of the enumeration.
S_OK | The number of objects returned was the same as the number requested. |
S_FALSE | The number of objects returned was less than the number requested. |
WBEM_S_TIMEDOUT | A timeout occurred before you obtained all the objects. |
On error, you can call the COM function GetErrorInfo to obtain more error information.
In the following sample, since objects are only requested one at a time, checking the return code for S_OK is correct:
void ListObjects(IEnumWbemClassObject *pEnum)
{
pEnum->Reset();
IWbemClassObject *pObject = 0;
ULONG uReturned;
while (pEnum->Next(INFINITE, 1, &pObject, &uReturned) == S_OK)
{
pObject->… // Access IWbemClassObject methods
pObject->Release();
}
}
In the next sample, Next may return S_FALSE, even though some objects are returned, so checking uReturned is required:
void ListObjects2(IEnumWbemClassObject *pEnum)
{
pEnum->Reset();
IWbemClassObject *paObjects[10];
ULONG uReturned;
HRESULT hRes;
while (1)
{
hRes = pEnum->Next(INFINITE, 10, paObjects, &uReturned);
if (uReturned == 0)
break;
for (ULONG u = 0; u < uReturned; u++)
{
IWbemClassObject *pObj = paObjects[u];
// ... use the object
pObj->Release();
}
}
// examine hRes to see if the termination finished normally
}