home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / adsi / sampapp / dsbrowse / vbads.bas < prev   
BASIC Source File  |  1997-07-29  |  6KB  |  206 lines

  1. Attribute VB_Name = "Module1"
  2. ' DSBROWSE
  3. ' This is a simple browser for Active Directory which demonstrates
  4. ' the use of the basic Active Directory Automation interfaces from
  5. ' VB. Browsing starts automatically at the Namespaces
  6. ' Collection Object. The user can expand the browser tree
  7. ' to any level desired. The properties option allows viewing
  8. ' of the current Active Directory object's properties, while the Set
  9. ' button allows changing property values.
  10.  
  11. Option Explicit
  12.  
  13. Public currentADsObj As IADs
  14. Public currentADsObjCont As IADsContainer
  15. Public currentADsObjSchema   As IADs
  16. Public currentNewObj   As IADs
  17. Public strPath As String
  18. Public bStartup As Boolean
  19. Public errstring As String
  20. Public ERRNUMB As Long
  21. Public bAbort As Boolean
  22. Public Sub enumerate_path(ByVal Node As Node)
  23. '
  24. 'Name: enumerate_path
  25. 'Input: Node As Node
  26. 'Desc: Does the actual enumerating of input node.
  27. '   All child object are enumerated and placed under
  28. '   the input node as children.
  29. 'Return: none
  30. '
  31.     
  32.     '
  33.     ' Set Error handler
  34.     '
  35.     On Error GoTo ErrorHandler
  36.     
  37.     Dim st As Variant
  38.     Dim nodX As Node    ' Create variable.
  39.     Dim ChildNode As Node    ' Create variable.
  40.     Dim I As Integer    ' Create a counter variable.
  41.     Dim Container As IADsContainer
  42.     Dim Class As IADsClass
  43.     Dim Child As IADs
  44.     Dim classobj As IADsClass
  45.     Dim tempClass As String
  46.     
  47.     bAbort = False
  48.     '
  49.     ' Get the object selected
  50.     '
  51.     Set currentADsObj = GetObject(strPath)
  52.     frmBrwsTree.txtName.Caption = currentADsObj.Name
  53.     frmBrwsTree.txtPath.Caption = currentADsObj.ADsPath
  54.     On Error Resume Next
  55.     '
  56.     ' Set the node if not passed into this sub as the current
  57.     ' node we are working on (for the collection coming up).
  58.     '
  59.     If Node Is Nothing Then
  60.        Set Node = frmBrwsTree.TreeView1.Nodes.Item(currentADsObj.ADsPath)
  61.        Node.Sorted = True
  62.     End If
  63.  
  64.     On Error GoTo ErrorHandler
  65.     
  66.     If frmBrwsTree.TreeView1.Nodes.Count = 0 Then
  67.         '
  68.         ' This means we are enumerating the namespaces container
  69.         ' No nodes expanded yet,
  70.         '
  71.         tempClass = SetClass(currentADsObj.Class)
  72.         Set nodX = frmBrwsTree.TreeView1.Nodes.Add(, , currentADsObj.ADsPath, _
  73.             currentADsObj.Name, tempClass)
  74.         nodX.Sorted = True
  75.         '
  76.         'Get namespace IDs
  77.         '
  78.         Set Container = currentADsObj
  79.         For Each Child In Container
  80.             tempClass = SetClass(Child.Class)
  81.             Set Node = frmBrwsTree.TreeView1.Nodes.Add(nodX, tvwChild, _
  82.                 Child.ADsPath, Child.Name, tempClass)
  83.             Node.Sorted = True
  84.             '
  85.             ' If the child is a container, then add a temporary nod
  86.             ' to show that it can be expanded later.
  87.             '
  88.             Set ChildNode = frmBrwsTree.TreeView1.Nodes.Add(Node, _
  89.                 tvwChild, Child.ADsPath & "/Dummy", "Dummy", tempClass)
  90.             ChildNode.Sorted = True
  91.         Next Child
  92.         bStartup = True 'disable expand processing
  93.         frmBrwsTree.TreeView1.Nodes(1).Expanded = True
  94.         bStartup = False
  95.         Set Node = Nothing
  96.     
  97.     Else
  98.     
  99.         Set Container = Nothing
  100.         Node.Sorted = True
  101.         '
  102.         ' Now, see if it's a container and if so, enumerate
  103.         ' all objects below it.
  104.         '
  105.         On Error Resume Next
  106.     
  107.         Set Container = currentADsObj
  108.         If Err = 0 Then
  109.             For Each Child In Container
  110.                 If Child Is Nothing Then Exit For
  111.                 Err = 0
  112.                 I = I + 1
  113.                 '
  114.                 ' Every 80 objects we check for user input
  115.                 '
  116.                 If I = 80 Then
  117.                     DoEvents
  118.                     If bAbort Then GoTo quitloop
  119.                     I = 0
  120.                 End If
  121.                 tempClass = SetClass(Child.Class) 'Gets the image
  122.                 If tempClass = "" Then tempClass = "Dummy"
  123.                
  124.                 Set nodX = frmBrwsTree.TreeView1.Nodes.Add(Node, tvwChild, _
  125.                     Child.ADsPath, Child.Name, tempClass)
  126.                 nodX.Sorted = True
  127.                 '
  128.                 ' If New node is a container then add temporary child node
  129.                 '
  130.                 If IsContainer(Child) Then
  131.                       
  132.                     Set ChildNode = frmBrwsTree.TreeView1.Nodes.Add(nodX, _
  133.                         tvwChild, Child.ADsPath & "Dummy", "Dummy", tempClass)
  134.                     ChildNode.Sorted = True
  135.                 End If
  136. skip:
  137.            Next Child
  138.         End If
  139.         
  140. quitloop:
  141.         Set currentADsObj = Nothing
  142. skip2:
  143.     End If
  144.     GoTo finish
  145.     
  146. ErrorHandler:
  147.     
  148.         errstring = "Had an error:" & Err.Number
  149.         ERRNUMB = Err.Number
  150.         MsgBox errstring
  151.         Resume Next
  152.  
  153. finish:
  154.  
  155. End Sub
  156. Public Function SetClass(classString As String) As String
  157. 'Name: SetClass
  158. 'Input: classString As String
  159. 'Desc: Used to determine what classname is
  160. '   used for the imagelist
  161. 'Return: SetClass As String
  162.  
  163.     SetClass = "img" & classString
  164.     
  165.     '
  166.     ' Evaluate input class to set image to all classes
  167.     '
  168.     Select Case classString
  169.     
  170.     Case "Computer"
  171.     Case "NameSpace"
  172.     Case "Namespaces"
  173.     Case "User"
  174.     Case "Domain"
  175.     Case "Service"
  176.     Case "Group"
  177.     Case "PrintQueue"
  178.     Case "FileShare"
  179.     Case "Organization"
  180.     Case "Country"
  181.     Case "Organizational Unit"
  182.         SetClass = "imgOrganization"
  183.     ' Here we set all others
  184.     Case Else
  185.         SetClass = "imgDefault"
  186.     End Select
  187.  
  188. End Function
  189.  
  190. Public Function IsContainer(objADS As IADs) As Boolean
  191. '
  192. ' This Function returns True or False depending on whether the
  193. ' object referred to by objADs is a container or not
  194. '
  195.     Dim Cont As IADsContainer
  196.     
  197.     On Error Resume Next
  198.     
  199.     Set Cont = objADS
  200.     If Err Then
  201.         IsContainer = False
  202.     Else
  203.         IsContainer = True
  204.     End If
  205. End Function
  206.