Solved

NT User Names and Groups

Posted on 2000-04-07
5
304 Views
Last Modified: 2008-03-06
How can I check the list of Users that can logon to NT and the groups that they belong to ?
0
Comment
Question by:yermaw
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
riduce earned 20 total points
ID: 2692580
Declare Function NetGetAnyDCName Lib "netapi32.dll" (servername As Byte, DomainName As
                    Byte, DCName As Long) As Long

                    Function GetAnyDCName(ByVal MName As String, ByVal DName As String) As String
                        Dim DCName As String, DCNPtr As Long
                        Dim DNArray() As Byte
                        Dim MNArray() As Byte
                        Dim DCNArray(100) As Byte
                        Dim Result As Long
                         
                        MNArray = MName & vbNullChar
                        DNArray = DName & vbNullChar
                        Result = NetGetAnyDCName(MNArray(0), DNArray(0), DCNPtr)
                        If Result <> 0 Then
                            MsgBox "Error: " & Result
                            Exit Function
                        End If
                        Result = PtrToStr(DCNArray(0), DCNPtr)
                        Result = NetAPIBufferFree(DCNPtr)
                        DCName = DCNArray()
                        GetAnyDCName = DCName
                         
                    End Function
0
 

Author Comment

by:yermaw
ID: 2692609
How does this code work ?

What is PtrToStr ?

Help !
0
 
LVL 1

Expert Comment

by:riduce
ID: 2692621
or :
Option Explicit

                    Private Type USER_INFO
                        Name As String
                        Comment As String
                        UserComment As String
                        FullName As String
                    End Type

                    Private Type USER_INFO_API
                        Name As Long
                        Comment As Long
                        UserComment As Long
                        FullName As Long
                    End Type


                    Private Declare Function NetUserEnum Lib "netapi32" _
                      (lpServer As Any, ByVal Level As Long, _
                       ByVal Filter As Long, lpBuffer As Long, _
                       ByVal PrefMaxLen As Long, EntriesRead As Long, _
                       TotalEntries As Long, ResumeHandle As Long) As Long
                       
                    Private Declare Function NetApiBufferFree Lib "netapi32" _
                       (ByVal pBuffer As Long) As Long

                    Private Declare Sub CopyMem Lib "kernel32" Alias _
                       "RtlMoveMemory" (pTo As Any, uFrom As Any, _
                        ByVal lSize As Long)
                         
                    Private Declare Function lstrlenW Lib "kernel32" _
                     (ByVal lpString As Long) As Long

                    Private Const NERR_Success As Long = 0&
                    Private Const ERROR_MORE_DATA As Long = 234&

                    Private Const FILTER_TEMP_DUPLICATE_ACCOUNT As Long = &H1&
                    Private Const FILTER_NORMAL_ACCOUNT As Long = &H2&
                    Private Const FILTER_PROXY_ACCOUNT As Long = &H4&
                    Private Const FILTER_INTERDOMAIN_TRUST_ACCOUNT As Long = &H8&
                    Private Const FILTER_WORKSTATION_TRUST_ACCOUNT As Long = &H10&
                    Private Const FILTER_SERVER_TRUST_ACCOUNT As Long = &H20&



                    Public Function GetUsers(UserNames() As String, _
                       Optional ServerName As String = "") As Boolean
                         
                        'PURPOSE: Get LoginNames of all users on the domain and
                        'save in a string array
                         
                        'PARAMETERS: UserNames(): Empty String array, passed byref,
                          'to hold user names
                         
                        'ServerName (Optional): Set to "" if you want user
                          'names for current machine, otherwise, set to the server
                          'you want (e.g., Domain Controller Name)
                         
                        'RETURNS: True if successful, false otherwise
                         
                        'EXAMPLE:
                            'Dim sUsers() As String
                            'dim lCtr as long
                            'GetUsers sUsers, "MyDomainController"
                             
                            'OR FOR LOCAL MACHINE
                             
                            'GetUsers sUsers
                       
                        'For lCtr = LBound(sUsers) To UBound(sUsers)
                         '   Debug.Print sUsers(lCtr)
                        'Next
                         
                         'NOTES: WINDOWS NT/2000 only (hasn't been tested on 2000)
                         
                        Dim lptrStrBuffer As Long
                        Dim lRet As Long
                        Dim lUsersRead As Long
                        Dim lTotalUsers As Long
                        Dim lHnd As Long
                        Dim etUserInfo As USER_INFO_API
                        Dim bytServerName() As Byte
                        Dim lElement As Long
                        Dim Users() As USER_INFO
                       
                        ReDim Users(0) As USER_INFO
                        ReDim UserNames(0) As String
                         
                        If Trim(ServerName) = "" Then
                            'Local users
                            bytServerName = vbNullString
                        Else
                            'Check the syntax of the ServerName string
                            If InStr(ServerName, "\\") = 1 Then
                                bytServerName = ServerName & vbNullChar
                            Else
                                bytServerName = "\\" & ServerName & vbNullChar
                            End If
                        End If
                        lHnd = 0
                        Do
                            'Begin enumerating users
                            If Trim(ServerName) = "" Then
                                lRet = NetUserEnum(vbNullString, 10, _
                                 FILTER_NORMAL_ACCOUNT, lptrStrBuffer, 1, _
                                  lUsersRead, lTotalUsers, lHnd)
                            Else
                                lRet = NetUserEnum(bytServerName(0), 10, _
                                 FILTER_NORMAL_ACCOUNT, lptrStrBuffer, 1, _
                                   lUsersRead, lTotalUsers, lHnd)
                            End If
                            'Populate UserInfo Structure
                            If lRet = ERROR_MORE_DATA Then
                                If UserNames(0) = "" Then
                                    lElement = 0
                                Else
                                    lElement = UBound(UserNames) + 1
                                End If
                                ReDim Preserve UserNames(lElement)
                                ReDim Preserve Users(lElement) As USER_INFO
                                 
                                CopyMem etUserInfo, ByVal lptrStrBuffer, _
                                   Len(etUserInfo)
                                'data of interest
                                Users(lElement).Name = PtrToString(etUserInfo.Name)

                                'Other stuff you can get, but not
                                'returned by this function
                                'modify this function if you are interested

                                Users(lElement).Comment = _
                                      PtrToString(etUserInfo.Comment)
                                Users(lElement).UserComment = _
                                     PtrToString(etUserInfo.UserComment)
                                Users(lElement).FullName = _
                                     PtrToString(etUserInfo.FullName)

                                UserNames(lElement) = Users(lElement).Name
                             
                            End If
                            If lptrStrBuffer Then
                                Call NetApiBufferFree(lptrStrBuffer)
                            End If
                            DoEvents
                        Loop While lRet = ERROR_MORE_DATA
                        GetUsers = True
                        Exit Function
                    ErrHandler:
                    On Error Resume Next
                    Call NetApiBufferFree(lptrStrBuffer)
                    End Function

                    Private Function PtrToString(lpString As Long) As String
                        'Convert a windows pointer to a VB string
                        Dim bytBuffer() As Byte
                        Dim lLen As Long
                         
                        If lpString Then
                            lLen = lstrlenW(lpString) * 2
                            If lLen Then
                                ReDim bytBuffer(0 To (lLen - 1)) As Byte
                                CopyMem bytBuffer(0), ByVal lpString, lLen
                                PtrToString = bytBuffer
                            End If
                        End If
                    End Function
0
 
LVL 1

Expert Comment

by:riduce
ID: 2692622
All code will give you usernames & groups

A good example aswell =

http://www.freevbcode.com/code/frmNetwork.zip
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 2694097
Try this


http://www.netfokus.dk/vbadmincode/codevb.html and look for 'account'

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now