Microsoft KB Archive/232485

From BetaArchive Wiki
Knowledge Base


How To Programmatically Create Members in Site Server 3.0 Using Java

Article ID: 232485

Article Last Modified on 9/11/2006



APPLIES TO

  • Microsoft Java Virtual Machine
  • Microsoft Visual J++ 6.0 Standard Edition
  • Microsoft Software Development Kit for Java 3.1
  • Microsoft Active Directory Service Interfaces 2.0
  • Microsoft Active Directory Service Interfaces 2.5



This article was previously published under Q232485

SUMMARY

This article describes how to programmatically create members in Site Server 3.0 Membership Directory, using Active Directory Service Interfaces (ADSI) 2.0 and 2.5, Java/COM integration, and J/Direct.

MORE INFORMATION

When you create a new Member object, the following three properties must be set:

  • objectClass
  • cn (Common Name)
  • GUID

The first two, objectClass and cn, are set when the Create() method is called. The third, GUID, must explicitly be set. If these properties are not set properly, the call to SetInfo() will fail and the new Member object will not be created.

The following code demonstrates adding a member programmatically:

import activeds.*;
//import autoobj.*;
//Uncomment the above line if you want to use
//early binding for the GuidGen object.
import com.ms.com.*;
import com.ms.win32.*;

public class ADSISamp
{
  IADsContainer oADsContainer;
  IADs oADsNewUser;
  Object oGuidGen;
  Variant strGuid;
  String strLdapPath;
  
  public static void main (String[] args) throws Exception
  {
    ADSISamp app = new ADSISamp();
    app.run();
    System.in.read();
  }
  
  public void run()
  {
    try 
    {
      //The path to the ou=Members container.
      strLdapPath = "LDAP://localhost:5292/o=Microsoft/ou=Members";
      //Instantiate the GUID Generator that comes with Site Server
      //and store the GUID for use later on.
      //Using CoCreateInstance here, but could 
      //also use a wrapper from autoobj.dll
      //in the Bin\P&M directory of the Site Server installation.
      oGuidGen = createObject("Membership.GuidGen.1");
      //oGuidGen = (IGuidGen)new GuidGen();
      //strGuid.putString(((IGuidGen)oGuidGen).GenerateGuid());
      
      //Using the IDispatch pointer returned from createObject
      //via the Dispatch class to call GenerateGuid.
      //Once again, could also use early-binding via the wrapper class.
      //Use of IDispatch allows us to use this object with out
      //importing the wrapper classes for autoobj.dll.
      strGuid = Dispatch.call(oGuidGen, "GenerateGuid");
      //Bind to the container in which the Member will be created.
      //Note that this is an API call, and is declared native 
      //with other API J/Direct declarations at the bottom of this
      //source file.  It does, however, return a COM Interface
      //exposed by the activeds.dll and described in activeds.tlb,
      //both of which can be found in the %windir%\system32 directory.
      oADsContainer = (IADsContainer)ADsGetObject(strLdapPath, IADs.iid);

      //Create the new user object.  Note that the Create() method
      //returns an Interface pointer that maps to the IADs wrapper 
      //class in Java, which is the core ADSI COM Interface.
      oADsNewUser = (IADs)oADsContainer.Create("member", "cn=JohnDoe");
      
      oADsNewUser.Put("givenName", new Variant("John"));
      oADsNewUser.Put("sn", new Variant("Doe"));
      oADsNewUser.Put("userPassword", new Variant("password"));
      //No need for a new Variant as Dispatch.call returns a Variant.
      oADsNewUser.Put("GUID", strGuid);
      oADsNewUser.SetInfo();
      
      //Destroy the objects.
      ComLib.release(oGuidGen);
      ComLib.release(oADsNewUser);
      ComLib.release(oADsContainer);
    }
    catch (Exception ex)
    {
      User32.MessageBox(0,
                        ex.toString(),
                        "Java Exception",
                        0);
    }
  }
  
  // J/Direct native API method declarations.
  //*******************************************************
  
  /** @dll.import("activeds", ole) */ 
  public static native IUnknown ADsGetObject(String pathname,
                                             _Guid riid);
  
  /** @dll.import("ole32", ole) */ 
  public static native IUnknown CoCreateInstance(_Guid clsid, 
                                                 IUnknown punkOuter, 
                                                 int context, 
                                                 _Guid riid); 
  /** @dll.import("ole32", ole) */ 
  public static native _Guid CLSIDFromString(String str);
  
  public static _Guid IID_IUnknown = new _Guid("{00000000-0000-0000-C000-000000000046}");
  
  public static Object createObject(String str) 
  {
    return CoCreateInstance(CLSIDFromString(str), 
                            null, 
                            ComContext.INPROC_SERVER | ComContext.LOCAL_SERVER, 
                            IID_IUnknown); 
  }  
}
                

REFERENCES

Microsoft Developer Network (MSDN) Library: "Active Directory Service Interfaces Version 2.0" located in SDK Documentation/Platform SDK/Networking and Distributed Services.

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Keywords: kbhowto kbjava kbjnative KB232485