Creating Applications with ADSI and VB

ADSI is a powerful interface with which you can access many NT features. Here is nine steps and an example of how you can use ADSI and VB to create applications

Ken Spencer

March 31, 1998

6 Min Read
ITPro Today logo in a gray background | ITPro Today

Life just got a little easier for application developers

If you've ever had to compile a list of users on your Windows NT system or domain, you probably had to resort to using User Manager or a tool from Microsoft Windows NT Server Resource Kit. Although some of NT's internal features (such as network connections) are easy to access, many others (such as the number of services started) are not. Microsoft is addressing access problems by adding a scripting language interface--the Active Directory Service Interfaces (ADSI)--to use with NT 5.0's new directory service, Active Directory.

ADSI is a powerful interface with which you can access many NT features. For example, you can use Visual Basic (VB) with ADSI to get a list of users from NT, without resorting to the NT API. You can also access information about domains, shares, and more.

You can use ADSI not only to access information, but also to create entities (such as users and shares) and to update existing information. You can even use ADSI to access information and create entities on different network operating systems.

Thanks to an incremental release strategy, Microsoft has already released ADSI 2.0 even though NT 5.0 is not available. Microsoft has also released Internet Information Server (IIS) 4.0, which contains the Windows Scripting Host. WSH is a scripting environment that lets you use various scripting languages with ADSI.

An Overview
If you haven't already done so, download ADSI 2.0 from Microsoft's Web site at http://backoffice.microsoft.com/downtrial/default.asp? product=19&item=205. Building applications with ADSI and VB involves three main tasks: creating a reference to the Active DS Type Library in VB, creating a reference to the object you're going to use, and creating the code that uses the object (and placing the code in a code module or class).

The first two tasks are relatively straightforward. The third task might be difficult if you are not familiar with ADSI and component object model (COM) objects. If you are unfamiliar with these subjects, you can consult Microsoft's white papers on ADSI at http://www.microsoft.com/ntserver/guide/asdiwp.asp.

A Specific Example
Here's an example of how you can use ADSI and VB to create applications. This application extracts the user accounts--including each user's full name, home directory, and logon script information--from an NT server called toshibaiis. The application then uses the FlexGrid control to display the retrieved user account information. Before starting this application, make sure you have installed ADSI on the system you use VB on. Then follow these nine steps.

Step 1
Create the application's shell in VB. Start VB, select Standard EXE from the New dialog box, and click OK. This step leaves VB open with one form (Form1).

Step 2
Create a reference to the ADSI object. From the Project menu, select References. You will see the References dialog box shown in Screen 1. Select the check box next to Active DS Type Library, and click OK.

Step 3
Place the FlexGrid control in the Toolbox. Right-click in the Toolbox, and select Components from the pop-up menu. From the Components property page, select the check box next to Microsoft FlexGrid Control 5.0, and then click OK.

Step 4
Place the CommandButton in the form. Click the CommandButton in the Toolbox, and drag out an area for this control on Form1. Click the CommandButton control once, then click the Caption property in the Properties window. Enter Load Users, and press return.

Step 5
Place the FlexGrid control in the form. Click the FlexGrid control in the Toolbox, and drag out an area for the control on Form1. Your form will look like Form1 in Screen 2.

Step 6
Name the CommandButton control (optional). Click the CommandButton control, and click the Name property in the Properties window. Enter the name cmdUsers, and press Return.

Step 7
Name the FlexGrid control (optional). Click the FlexGrid control, and click the Name property in the Properties window. Enter the name flxUsers, and press Return.

Step 8
Enter the VB code for the application. From the View menu, select Code to display the source code for the application. Enter the code in Listing 1 in the General Declarations section, and enter the code in Listing 2 in the Form_Load event.

The code in General Declarations creates references to ADSI objects that you will use. (If you are unfamiliar with ADSI object hierarchy, see Sakari Kouti, "Manage Directory Resources with Active Directory Service Interfaces," November 1997.) The code in Form_Load sets up the interface for the FlexGrid control.

Step 9
Enter the command code for the application. Enter the code in Listing 3 in the cmdUsers_Click event procedure. This code does all the work. First, the code tells the application which NT server to search. (In this example, the server is toshibaiis. However, you need to enter the appropriate NT server name for your network.) Then the code tells the application to loop through the container (oContainer) looking for users.

In this loop, the application checks the oIADs.Class property to determine whether the object is a user account reference (and not a service or group). Next the application sets the oUser reference to oIADs, which represents the user. The application then extracts the Name (username), FullName, HomeDirectory, and LoginScript information. Finally, the application uses the Additem method of the FlexGrid to add the information to the grid. (The single line of code in the Else clause simply prints the current class in the Debug window during testing.) You can see the results of running this application in Screen 3.

ADSI: An Important, Easy-to-Use Tool
ADSI provides you with an easy-to-use interface for various objects within the NT operating system. As NT plays an increasingly critical role in enterprises, so too will ADSI and scripting languages such as VBScript. ADSI will become an important tool for managing and controlling your systems. In addition, with VB and VB scripting tools, ADSI becomes a powerful tool with which you can access a variety of network resources that were previously available only using the more complex network APIs.

ADSI and WSH: A Natural Pair

Because ADSI and WSH provide a COM interface to system services, they compliment each other. WSH provides the scripting environment, and ADSI provides the objects to access internal NT features.WSH, which runs on NT 4.0 or Windows 95, provides both a command-prompt and Windows-based script engine in which you can run VBScript, Jscript, and many other scripting languages. You can implement the engines with two executables: wscript.exe and cscript.exe. Both engines can execute the same code, unless something about the code will work in only one or the other. WSH also includes the Windows Script Object, which lets you access Network, Share, and other resources. (For more information about WSH, go to Microsoft's Web site at http://www.microsoft.com/management/scrpthost.htm.)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like