Results 1 to 4 of 4

Thread: Domains List

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    10

    Domains List

    Hi,
    Is there an API to get the list of domains in a given environment?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Domains List

    Here is one of my code examples that will populate a combo with all the available Domains and another
    combo with the available computers in the selected domain.

    VB Code:
    1. 'IN A MODULE
    2. Option Explicit
    3.  
    4. Public Type SERVER_INFO_100
    5.     sv100_platform_id                    As Long
    6.     sv100_name                           As Long
    7. End Type
    8.  
    9. Public Const SV_TYPE_DOMAIN_ENUM         As Long = &H80000000
    10. Public Const SV_TYPE_ALL                 As Long = &HFFFFFFFF
    11. Public Const SV_TYPE_SQLSERVER           As Long = &H4
    12. Public Const MAX_PREFERRED_LENGTH        As Long = -1
    13. Public Const NERR_SUCCESS                As Long = 0&
    14. Public Const ERROR_REQ_NOT_ACCEP         As Long = 71&
    15. Public Const ERROR_MORE_DATA             As Long = 234&
    16. Public Const NERR_BASE                   As Long = 2100
    17. Public Const NERR_InvalidComputer = (NERR_BASE + 251)
    18.  
    19. Public Declare Function NetServerEnum Lib "netapi32" (ByVal servername As Long, ByVal level As Long, buf As Any, _
    20. ByVal prefmaxlen As Long, entriesread As Long, totalentries As Long, ByVal servertype As Long, ByVal domain As Long, _
    21. resume_handle As Long) As Long
    22.  
    23. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
    24.    
    25. Public Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Long) As Long
    26.  
    27. Public Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    28.  
    29. Public Function GetServers(sDomain As String, sType As Long) As String()
    30.  
    31.     Dim bufptr              As Long
    32.     Dim dwEntriesread       As Long
    33.     Dim dwTotalentries      As Long
    34.     Dim dwResumehandle      As Long
    35.     Dim se100               As SERVER_INFO_100
    36.     Dim success             As Long
    37.     Dim nStructSize         As Long
    38.     Dim Cnt                 As Long
    39.    
    40.     nStructSize = LenB(se100)
    41.     success = NetServerEnum(0&, 100, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, sType, StrPtr(sDomain), dwResumehandle)
    42.     'success = 71 'NO ACTIVE 'DOMAIN'
    43.     If success = NERR_SUCCESS And success <> ERROR_MORE_DATA Then
    44.         Dim compNames() As String
    45.         ReDim compNames(dwEntriesread)
    46.         For Cnt = 0 To dwEntriesread - 1
    47.             CopyMemory se100, ByVal bufptr + (nStructSize * Cnt), nStructSize
    48.             compNames(Cnt) = GetPointerToByteStringW(se100.sv100_name)
    49.             DoEvents
    50.         Next
    51.     End If
    52.     Call NetApiBufferFree(bufptr)
    53.     GetServers = compNames
    54.    
    55. End Function
    56.  
    57. Public Function GetPointerToByteStringW(ByVal dwData As Long) As String
    58.  
    59.     Dim tmp() As Byte
    60.     Dim tmplen As Long
    61.    
    62.     If dwData <> 0 Then
    63.         tmplen = lstrlenW(dwData) * 2
    64.         If tmplen <> 0 Then
    65.             ReDim tmp(0 To (tmplen - 1)) As Byte
    66.             CopyMemory tmp(0), ByVal dwData, tmplen
    67.             GetPointerToByteStringW = tmp
    68.         End If
    69.     End If
    70.    
    71. End Function
    Usage:
    VB Code:
    1. 'BEHIND A FORM WITH TWO COMBO'S (cboDomain and cboComputer)
    2. Private Sub Form_Load()
    3.  
    4.     Dim compNames() As String
    5.  
    6.     'SETUP THE DOMAIN COMBO BOX
    7.     compNames = GetServers(vbNullString, SV_TYPE_DOMAIN_ENUM)
    8.     For x = LBound(compNames) To UBound(compNames) - 1
    9.         cboDomain.AddItem compNames(x)
    10.     Next
    11.     cboDomain.ListIndex = 0
    12.  
    13. End Sub
    14.  
    15. Private Sub cboDomain_Click()
    16.     'NEED TO CLEAR CBOCOMPUTER AND RELOAD WITH COMPUTERS IN THE NEWLY SELECTED DOMAIN
    17.     'SETUP THE COMPUTER COMBO BOX
    18.     On Error GoTo No_Bugs
    19.    
    20.     Dim compNames() As String
    21.     Dim compName As String
    22.     Dim x As Integer
    23.    
    24.     cboComputer.Clear
    25.     x = 0
    26.     compNames = GetServers(cboDomain.Text, SV_TYPE_ALL)
    27.     If UBound(compNames) > 0 Then
    28.         For x = LBound(compNames) To UBound(compNames) - 1
    29.             cboComputer.AddItem compNames(x)
    30.         Next
    31.         cboComputer.ListIndex = 0
    32.     End If
    33.     Exit Sub
    34.    
    35. No_Bugs:
    36.     If Err.Number = "9" Then
    37.         cboDomain.RemoveItem (cboDomain.ListIndex)
    38.         cboDomain.ListIndex = 0
    39.         compNames = GetServers(cboDomain.Text, SV_TYPE_ALL)
    40.         Resume
    41.     Else
    42.         MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation, App.ProductName
    43.     End If
    44.    
    45. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    10

    Resolved Re: Domains List

    Oh wow.. this more than I wanted.. Thanks a ton

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Domains List

    No prob I worked on this a couple of years ago so it was no effort on my part

    Glad it helps you though.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width