Microsoft KB Archive/194115

= How To Use ADSI in VB to Enumerate the Computers in an NT Domain =

Article ID: 194115

Article Last Modified on 3/3/2005

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Active Directory Service Interfaces 2.0
 * Microsoft Active Directory Service Interfaces 2.5

-



This article was previously published under Q194115



SUMMARY
You can use the Microsoft Active Directory Service Interfaces (ADSI) to easily enumerate the computers in an NT Domain. Simply create an IADsDomain object (the domain) and an IADsComputer object (a computer),and then perform a loop for each computer object in the domain object. The domain object is a container of the computer objects.



MORE INFORMATION
An ADSI container object contains other objects. A container object controls the objects within it, providing methods to create, filter, retrieve, copy, move and delete the objects. A container object is different from a Collection, which is a more limited set of items and methods. Container objects are used to represent the hierarchical structures in the Active Directory Services.

One example of the differences between a Container and a Collection is in enumeration of member objects. The syntax for enumerating the objects in a container, for example, is: For Each Object In ContainerObject An example of this is found in the code example: For Each Computer In TheDomain The syntax for enumerating the objects in a Collection is: For Each Object In Collection.Objects An example of this is enumerating the Controls collection of a form: For Each ctl In Form1.Controls The IADsDomain object is a ADS container object that represents a domain on a network.

The IADsComputer object represents the information about a computer on a network. In some directory services, such as Windows NT, the computer object can also be a container object, containing the services that the computer provides.

Step-by-Step Example
To use the ADSI 2.0 object model, your system must have the ADSI run-time installed. The run-time and Help file are available at"

http://www.microsoft.com/backoffice/downloads.htm

Once you have installed the ADSI run-time, you will have the Active DS Type Library (ActiveDS.tlb) available. The Active DS Type Library provides the ADSI object model.

 Create a Standard EXE project in Visual Basic. Form1 is created by default. Set a reference to the Active DS Type Library by selecting Project, References, and then selecting Active DS Type Library.

NOTE: You may not see the Active DS Type Library in the list of available References. To add the Library to the list:

 Click Browse. Go to the Windows\System folder or WinNT\System32 folder. Select the file ActiveDS.tlb. From now on, the Active DS Type Library is among the list of available References.</ol> </li> Add a ListBox and CommandButton to Form1.</li>  Add the following code to Form1's Code Window. Private Sub Command1_Click

Dim TheDomain As IADsDomain Dim Computer As IADsComputer Dim strDomain As String Dim iMousePointer As Integer

'Accept the Domain name strDomain = InputBox("Domain Name: ")

'Use the WinNT Directory Services strDomain = "WinNT://" & strDomain

'Initialize the User Interface List1.Clear iMousePointer = Me.MousePointer Me.MousePointer = vbHourglass

'Create the Domain object Set TheDomain = GetObject(strDomain)

'Search for Computers in the Domain TheDomain.Filter = Array("Computer")

For Each Computer In TheDomain List1.AddItem Computer.Name Next Computer

'Clean up    Me.MousePointer = iMousePointer Set Computer = Nothing Set TheDomain = Nothing

End Sub 'Command1_Click </li> Run the test project by doing the following:

<ol style="list-style-type: lower-alpha;"> Click CommandButton.</li> When prompted, enter the Domain name. Depending on the number of computers in the domain, the enumeration may take a while.</li></ol> </li></ol>

<div class="references_section">