In this section, you will begin an exercise that will yield the creation of the LDAPAdmin.DLL COM server.
Create a new ActiveX DLL Visual Basic project.
Set a reference to the Active DS Type Library by clicking the Project menu, selecting References[el], and placing a checkmark next to the "Active DS Type Library" entry. Also set a reference to the "Microsoft ADO". Click the OK command button to exit the References “Project1 dialog box.
Rename Project1 as LDAPAdmin .
Rename the Class1 class module as LDAPObjectManagement .
Enter the following code into the General Declarations section of the class module:
Public Function ModifyObjectAttribute(ByVal ObjectDistinguishedName As String, ByVal DirectoryAdminUsername As String, ByVal DirectoryAdminPassword As String, ByVal AttributeName As String, ByVal NewAttributeValue As Variant) As Boolean Dim dso As IADsOpenDSObject Dim Obj As IADs Set dso = GetObject("LDAP:") Set Obj = dso.OpenDSObject(ObjectDistinguishedName, DirectoryAdminUsername, DirectoryAdminPassword, 0) Call Obj.Put(AttributeName, NewAttributeValue) Obj.SetInfo If Err.Number = 0 Then ModifyObjectAttribute = True End Function Public Function CreateNewLDAPObject(ByVal ParentContainerDistinguishedName As String, ByVal DirectoryAdminUsername As String, ByVal DirectoryAdminPassword As String, ByVal ObjectRelativeName As String, ByVal ObjectClass As String,Optional ByVal MandatoryAttributeName1 As String, Optional ByVal MandatoryAttributeValue1 As Variant, Optional ByVal MandatoryAttributeName2 As String, Optional ByVal MandatoryAttributeValue2 As Variant, Optional ByVal MandatoryAttributeName3 As String, Optional ByVal MandatoryAttributeValue3 As Variant, Optional ByVal MandatoryAttributeName4 As String, Optional ByVal MandatoryAttributeValue4 As Variant, Optional ByVal MandatoryAttributeName5 As String, Optional ByVal MandatoryAttributeValue5 As Variant) As Boolean Dim dso As IADsOpenDSObject Dim Obj As IADs Dim ParentObj As IADs Set dso = GetObject("LDAP:") Set ParentObj = dso.OpenDSObject(ParentContainerDistinguishedName, DirectoryAdminUsername, DirectoryAdminPassword, 0) Set Obj = ParentObj.Create(ObjectClass, ObjectRelativeName) If MandatoryAttributeName1 <> "" Then Obj.Put MandatoryAttributeName1, MandatoryAttributeValue1 End If If MandatoryAttributeName2 <> "" Then Obj.Put MandatoryAttributeName2, MandatoryAttributeValue2 End If If MandatoryAttributeName3 <> "" Then Obj.Put MandatoryAttributeName3, MandatoryAttributeValue3 End If If MandatoryAttributeName4 <> "" Then Obj.Put MandatoryAttributeName4, MandatoryAttributeValue4 End If If MandatoryAttributeName5 <> "" Then Obj.Put MandatoryAttributeName5, MandatoryAttributeValue5 End If Obj.SetInfo If Err.Number = 0 Then CreateNewLDAPObject = True End Function Public Function RemoveLDAPObject(ByVal ParentContainerDistinguishedName As String, ByVal DirectoryAdminUsername As String, ByVal DirectoryAdminPassword As String, ByVal ObjectRelativeName As String, ByVal ObjectClass As String) As Boolean Dim dso As IADsOpenDSObject Dim Obj As IADs Dim ParentObj As IADs Set dso = GetObject("LDAP:") Set ParentObj = dso.OpenDSObject(ParentContainerDistinguishedName, DirectoryAdminUsername, DirectoryAdminPassword, 0) Call ParentObj.Delete(ObjectClass, ObjectRelativeName) If Err.Number = 0 Then RemoveLDAPObject = True End Function Public Function EnumerateLDAPContainer(ByVal ContainerDistinguishedName As String, ByVal DirectoryAdminUsername As String, ByVal DirectoryAdminPassword As String) As Variant On Error Resume Next Dim dso As IADsOpenDSObject Dim Obj As IADs Dim Item As IADs Dim Counter As Long Dim ReturnArray() As Variant Set dso = GetObject("LDAP:") Set Obj = dso.OpenDSObject(ContainerDistinguishedName, DirectoryAdminUsername, DirectoryAdminPassword, 0) For Each Item In Obj Counter = UBound(ReturnArray) + 1 ReDim Preserve ReturnArray(Counter) ReturnArray(Counter) = Item.Name Next EnumerateLDAPContainer = ReturnArray End Function Public Function SearchLDAPNamespace(ByVal SQLStmt As String) As Variant On Error Resume Next Dim Connection As ADODB.Connection Dim RS As ADODB.Recordset Dim Entry As String Dim Index As Long Dim ReturnArray() As Variant Dim Counter As Long Index = 0 Set Connection = New ADODB.Connection Connection.Provider = "ADsDSOObject" Connection.Open "ADSI" 'Example SQLStmt: SELECT cn,telephonenumber FROM 'LDAP://LDAP_SERVER/o=airius.com/ou=people' WHERE sn='carter' Set RS = Connection.Execute(SQLStmt) While Not RS.EOF For i = 0 To RS.Fields.Count - 1 If RS.Fields(i).Type = adVariant And Not (IsNull(RS.Fields(i).Value)) Then For j = LBound(RS.Fields(i).Value) To UBound(RS.Fields(i).Value) Entry = Entry&RS.Fields(i).Value(j)&vbTab Next j Else Entry = Entry&RS.Fields(i).Value&vbTab End If If Index = RS.Fields.Count - 1 Then Counter = UBound(ReturnArray) + 1 ReDim Preserve ReturnArray(Counter) ReturnArray(Counter) = Entry End If Index = Index + 1 Next i Entry = "" Index = 0 RS.MoveNext Wend SearchLDAPNamespace = ReturnArray End Function
Compile the code as LDAPAdmin.DLL.
Save and close the LDAPAdmin project.
Tip
If you do not want to share your code between applications, you can enter the preceding code into a code module in any Visual Basic application .
Tip
You can download the Visual Basic 6.0 project or precompiled version of LDAPAdmin.DLL from http://www.newriders.com/adsi.
With the LDAPObjectManagement class module created, you can access the functions contained in the class module from any programming language that supports OLE automation including Visual Basic, VBScript, and JavaScript.
Tip
To instantiate the object, follow the appropriate syntax found in Chapter 3, "Container Enumeration Methods and Programmatic Domain Account Policy Manipulation." Substitute the LDAPObjectManagement class name where necessary .
Use Table 11.1 to help you use the proper syntax for each of the methods of the LDAPObjectManagement interface.
Action | Syntax |
---|---|
Modify Object Attribute | Modify Object Attribute ("LDAP://Directory_ Server/o=airius.com/ou=people/uid=jlutz", "cn=directory manager", "l@undrym@t1962", "sn", "London") |
Create Generic Object in Namespace | Debug.Print CreateNewLDAPObject LDAP ("LDAP://Directory_Server/0=airius.com/ ou=people", "cn=directory manager", "l@undrym@t1962", "uid=teck", "inetOrgPerson", "cn", "Thomas Eck", "sn", "Eck", "givenName", "Thomas") |
Enumerate Container Object | For Each Item In EnumerateLDAPContainer ( "LDAP://Directory_Server/o=airius.com/ou=people", "cn=directory manager", "l@undrym@t1962") Debug.Print Item Next |
Remove LDAP Object From Namespace | Debug.Print RemoveLDAPObject ("LDAP://Directory_Server/o= airius.com/ou=people", "cn=directory manager", "l@undrym@t1962", "uid=eckth", "inetOrgPerson") |
Search LDAP Namespace By Namespace | For Each ObjName In SearchLDAP Attribute Value ("SELECT cn,telephonenumber FROM 'LDAP://Directory_Server/ o=airius.com/ou=people' WHERE sn='carter'") Debug.Print ObjName Next |
Top |