Platform SDK: Active Directory, ADSI, and Directory Services

Programming ADSI with Java/COM

Using the Microsoft virtual machine for Java (Microsoft VM) and Microsoft Java Compiler, you have access to all the ADSI features, as exposed through any ADSI COM components, from a Java/COM application. The following simple Java code example illustrates the necessary elements needed to bind to an ADSI object and invoke methods on that object. The required ADSI API functions and object methods are exposed through activeds.dll.

import activeds.*;                 // ADSI COM Wrapper classes
import com.ms.com.*;               // to use _Guid data type in COM.

public Class SimpleADSI 
{
    IADs obj;
    String path = "WinNT://domain/machine,computer";
    _Guid riid = IADs.iid;
    public static void main(String args[]) 
    {
        try 
        {
            obj = (IADs)ADsGetObject(path, riid);
            System.out.println( "Object name:  " + obj.getName() );
            System.out.println( "      class:  " + obj.getSchema() );
            System.out.println( "    ADsPath:  " + obj.getADsPath() );
            System.out.println( "     parent:  " + obj.getParent() );
        }
        catch (Exception e) 
        {
            System.out.println( "SimpleADSI Error: " + e.toString() );
        }
    }

    /** @dll.import("activeds", ole) */
    private static native IUnknown ADsGetObject(String path, _Guid riid);
}

The argument in the first import statement refers to Java Wrapper classes packaged in activeds.dll. You can use Visual J++ to create the wrapper classes and include them in your project, following the instructions below:

To create wrapper classes and include them in your project

  1. In a Visual J++ project, select the Add Com Wrapper... item from the Project menu,
  2. Select "Active DS Type Library" from the Installed Components: from the COM Wrappers dialog. If the type library is not shown in the list box, click the Browse... button, navigate to the directory where activeds.tlb is, and then select the type library.
  3. Click the OK button.

Visual J++ will create the activeds package for the Java Wrapper classes and include the package into the project's default path. You should see the activeds package in the Project Explore pane on the Visual J++ window.

To get an ADSI object that cannot be co-created, you must use one of the exposed ADSI API functions, for example, ADsGetObject or ADsOpenObject, which are also packaged in activeds.dll. Microsoft J/Direct makes it a simple task to access these and other native APIs. This is illustrated by the last two lines of the code example given above.

When compiling, make sure that Microsoft Language Extension is enabled. To do this, select <project>Properties... from the Project menu in the Visual J++ project window. Then, click the Compile tab in the <project> Properties dialog. Deselect Disable Microsoft Language Extensions. If compiling from the command line, use "/x-" switch, for example:

jvc /x- SimpleADSI.java

Finally, in order for the virtual machine to load the COM component, the dynamic-link library must be visible on the system path. If you get a "java.lang.UnsatisfiedLinkError" error, try to set the PATH to include the path containing the required dll. For example, if activeds.dll has been installed in c:\adsi\lib, you may want to issue the following command from the command line:

set PATH = %PATH%; c:\adsi\lib