DIWbemServices.CreateInstanceEnum

[This is preliminary documentation and subject to change.]

The DIWbemServices.CreateInstanceEnum method creates an enumerator that returns the instances of a specified class according to user-specified selection criteria. This method implements a simple query; more complex queries may require the use of DIWbemServices.ExecQuery.

Note that it is not an error for the returned enumerator to have zero elements

DIWbemServices.CreateInstanceEnum(
  [in] Class As String, 
  [in] lFlags As Long, 
  [in] pCtx As Object, 
  [out] ppEnum  As Object
) As Long
 

Parameters

Class
A string containing the name of the class for which instances are desired. This parameter cannot be vbNullString.
lFlags
WBEM_FLAG_DEEP forces recursive enumeration into all subclasses derived from the specified class. Instances of Class as well as those of its derived classes return. WBEM_FLAG_SHALLOW forces the enumeration to include only the instances of the specified class.
pCtx
Typically NOTHING. Otherwise, this is a DWbemContext object required by the dynamic class provider that is producing the class instances. You must specify the values in the context object in the documentation for the provider in question.
ppEnum
Object of type DIEnumWbemClassObject that receives the enumerator.

Return Values

WBEM_E_ACCESS_DENIED The current user does not have permission to view instances of the specified class.
WBEM_E_FAILED Unspecified error.
WBEM_E_INVALID_CLASS The specified class is invalid.
WBEM_E_INVALID_PARAMETER An invalid parameter was specified, or the namespace could not be parsed.
WBEM_E_OUT_OF_MEMORY There was not enough memory to complete the operation.
WBEM_NO_ERROR Success.

For more information on return values see Visual Basic error handeling and return values

Sample Code

Private Sub gather_drive_data()
' This example populates a list box named List1 with instances of
' logical drives using Win32_LogicalDisk.
  Dim ppEnum As DIEnumWbemClassObject ' The enumerator
  Dim ppObject As DWbemClassObject
  Dim pVal As Variant
' Use a custom function named wbem_logon to log on to the
' namespace using Dwbemlocator.ConnectServer.

  If wbem_logon <> WBEM_NO_ERROR Then     
     msgbox("Unable to log on to namespace")
     exit sub
  endif

' To create an enumeration object for Win32_LogicalDisk use
' DIWbemServices.CreateInstanceEnum.
' lFlags is set to WBEM_FLAG_DEEP to force recursive enumeration
' into all subclasses derived from the specified class.
' ppNamespace is a DIWbemServices type object bound to the 
' specified namespace in wbem_logon

  ppNamespace.CreateInstanceEnum "Win32_LogicalDisk", WBEM_FLAG_DEEP, Nothing, ppEnum

' ppEnum now contains the enumerator. Next, copy all the instances of ' Win32_LogicalDisk into a listbox using the Next method.
' Pass in the name of the property we want to get the value of.
' Pass in 0 for the other arguments except pval.
' pval receives the actual value of the property

  While ppEnum.Next(100, ppObject) <> WBEM_S_FALSE
        ppObject.Get "__RELPATH", 0, pVal, 0, 0
        List1.AddItem pVal 'Add to the listbox
  Wend
  Set ppEnum = Nothing     'Set the objects back to nothing
End Sub

See Also

Error Objects, DIWbemServices.CreateInstanceEnumAsync,DWbemLocator.ConnectServer