How can I get users from a domain?
How can i get a list of all NT users in a NT domain?
thanks all.
Salvino
[85 byte] By [
salvino] at [2007-11-10 0:19:36]

# 1 Re: How can I get users from a domain?
Salvino,
>How can i get a list of all NT users in a NT domain?
You're running on NT, I assume? If so, here's the relevant portion of
a users collection class.
'==================================================
' Module: CUsers
'
' Description: This is a collection of the
' users defined for a particular machine.
'
' Programmer: Craig Clearman
'
' Revision History:
' 30 Apr 96 - Module Created
'
'==================================================
'==================================================
' Private API Structures
'==================================================
Private Type USER_INFO_0
psUserName As Long
End Type
'==================================================
' Private API Declarations
'==================================================
Private Declare Sub CopyMem _
Lib "kernel32" Alias "RtlMoveMemory" ( _
pTo As Any, _
pFrom As Any, _
ByVal nCount As Long)
Private Declare Function lstrlenW _
Lib "kernel32" ( _
ByVal pString As Long) As Long
Private Declare Function NetApiBufferFree _
Lib "netapi32" ( _
ByVal pBuffer As Long) As Long
Private Declare Function NetUserEnum _
Lib "netapi32" ( _
ByVal psServer As Long, _
ByVal hLevel As Long, _
ByVal hFilter As Long, _
pBuffer As Long, _
ByVal nMaxLength As Long, _
nEntries As Long, _
nTotal As Long, _
hResume As Long) As Long
'==================================================
' Private API Constants
'==================================================
Private Const ERROR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
'==================================================
' Private Member Data
'==================================================
Private m_usersCollection As Collection
'==================================================
' Private Procedures
'==================================================
'==================================================
' Procedure: LoadUsers
'
' Description: This procedure will walk through
' the users on a particular machine and add them
' onto the collection.
'==================================================
Private Sub LoadUsers()
Const MAX_SIZE As Long = 2048&
Dim uUsers() As USER_INFO_0
Dim hReturn As Long
Dim pBuffer As Long
Dim nEntries As Long
Dim nTotal As Long
Dim hResume As Long
Dim iUser As Long
Dim userEnum As CUser
Dim sUserID As String
If m_usersCollection Is Nothing Then
Set m_usersCollection = New Collection
Do
If Len(m_sServer) Then
hReturn = NetUserEnum(StrPtr(m_sServer), 0&, 0&, pBuffer,
MAX_SIZE, nEntries, nTotal, hResume)
Else
hReturn = NetUserEnum(ByVal 0&, 0&, 0&, pBuffer, MAX_SIZE,
nEntries, nTotal, hResume)
End If
If hReturn = ERROR_MORE_DATA Or hReturn = ERROR_SUCCESS Then
If pBuffer Then
If nEntries Then
ReDim uUsers(0 To nEntries - 1) As USER_INFO_0
CopyMem uUsers(0), ByVal pBuffer, Len(uUsers(0)) *
nEntries
For iUser = 0 To nEntries - 1
Set userEnum = New CUser
With uUsers(iUser)
sUserID = WidePointerToString(.psUserName)
End With
userEnum.Init sUserID
m_usersCollection.Add userEnum, sUserID
Next iUser
End If
NetApiBufferFree pBuffer
pBuffer = 0&
End If
End If
Loop While hReturn = ERROR_MORE_DATA
End If
End Sub
'==================================================
' Procedure: WidePointerToString
'
' Inputs: pString A pointer to a Unicode
' string
'
' Returns: String
'
' Description: This procedure will turn a pointer
' representation of a string, often from API functions,
' and turn it into a string that VB can understand.
'==================================================
Private Function WidePointerToString(ByVal pString As Long) As String
Dim sString As String
Dim nChars As Long
If pString Then
'How many characters do I need?
nChars = lstrlenW(pString)
If nChars Then
sString = Space$(nChars)
'Multiply the characters by two, to get
' the Unicode size in bytes.
CopyMem ByVal StrPtr(sString), ByVal pString, nChars * 2
End If
End If
WidePointerToString = sString
End Function
Ciao, Craig
# 2 Re: How can I get users from a domain?
Salvino,
>How can i get a list of all NT users in a NT domain?
You're running on NT, I assume? If so, here's the relevant portion of
a users collection class.
'==================================================
' Module: CUsers
'
' Description: This is a collection of the
' users defined for a particular machine.
'
' Programmer: Craig Clearman
'
' Revision History:
' 30 Apr 96 - Module Created
'
'==================================================
'==================================================
' Private API Structures
'==================================================
Private Type USER_INFO_0
psUserName As Long
End Type
'==================================================
' Private API Declarations
'==================================================
Private Declare Sub CopyMem _
Lib "kernel32" Alias "RtlMoveMemory" ( _
pTo As Any, _
pFrom As Any, _
ByVal nCount As Long)
Private Declare Function lstrlenW _
Lib "kernel32" ( _
ByVal pString As Long) As Long
Private Declare Function NetApiBufferFree _
Lib "netapi32" ( _
ByVal pBuffer As Long) As Long
Private Declare Function NetUserEnum _
Lib "netapi32" ( _
ByVal psServer As Long, _
ByVal hLevel As Long, _
ByVal hFilter As Long, _
pBuffer As Long, _
ByVal nMaxLength As Long, _
nEntries As Long, _
nTotal As Long, _
hResume As Long) As Long
'==================================================
' Private API Constants
'==================================================
Private Const ERROR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
'==================================================
' Private Member Data
'==================================================
Private m_usersCollection As Collection
'==================================================
' Private Procedures
'==================================================
'==================================================
' Procedure: LoadUsers
'
' Description: This procedure will walk through
' the users on a particular machine and add them
' onto the collection.
'==================================================
Private Sub LoadUsers()
Const MAX_SIZE As Long = 2048&
Dim uUsers() As USER_INFO_0
Dim hReturn As Long
Dim pBuffer As Long
Dim nEntries As Long
Dim nTotal As Long
Dim hResume As Long
Dim iUser As Long
Dim userEnum As CUser
Dim sUserID As String
If m_usersCollection Is Nothing Then
Set m_usersCollection = New Collection
Do
If Len(m_sServer) Then
hReturn = NetUserEnum(StrPtr(m_sServer), 0&, 0&, pBuffer,
MAX_SIZE, nEntries, nTotal, hResume)
Else
hReturn = NetUserEnum(ByVal 0&, 0&, 0&, pBuffer, MAX_SIZE,
nEntries, nTotal, hResume)
End If
If hReturn = ERROR_MORE_DATA Or hReturn = ERROR_SUCCESS Then
If pBuffer Then
If nEntries Then
ReDim uUsers(0 To nEntries - 1) As USER_INFO_0
CopyMem uUsers(0), ByVal pBuffer, Len(uUsers(0)) *
nEntries
For iUser = 0 To nEntries - 1
Set userEnum = New CUser
With uUsers(iUser)
sUserID = WidePointerToString(.psUserName)
End With
userEnum.Init sUserID
m_usersCollection.Add userEnum, sUserID
Next iUser
End If
NetApiBufferFree pBuffer
pBuffer = 0&
End If
End If
Loop While hReturn = ERROR_MORE_DATA
End If
End Sub
'==================================================
' Procedure: WidePointerToString
'
' Inputs: pString A pointer to a Unicode
' string
'
' Returns: String
'
' Description: This procedure will turn a pointer
' representation of a string, often from API functions,
' and turn it into a string that VB can understand.
'==================================================
Private Function WidePointerToString(ByVal pString As Long) As String
Dim sString As String
Dim nChars As Long
If pString Then
'How many characters do I need?
nChars = lstrlenW(pString)
If nChars Then
sString = Space$(nChars)
'Multiply the characters by two, to get
' the Unicode size in bytes.
CopyMem ByVal StrPtr(sString), ByVal pString, nChars * 2
End If
End If
WidePointerToString = sString
End Function
Ciao, Craig
# 3 Re: How can I get users from a domain?
I would like to get this code to work as I have needed this before. In the
code you posted you are missing a variable declaration for 'm_sServer',
I was hoping you could tell me what it is.
"Craig Clearman" <chclear@nospam.please> wrote in message
news:iegscs0bn3jf8bfsskt4tm3utp8gmu76sg@4ax.com...
> Salvino,
>
> >How can i get a list of all NT users in a NT domain?
>
> You're running on NT, I assume? If so, here's the relevant portion of
> a users collection class.
>
> '==================================================
> ' Module: CUsers
> '
> ' Description: This is a collection of the
> ' users defined for a particular machine.
> '
> ' Programmer: Craig Clearman
> '
> ' Revision History:
> ' 30 Apr 96 - Module Created
> '
> '==================================================
>
> '==================================================
> ' Private API Structures
> '==================================================
> Private Type USER_INFO_0
> psUserName As Long
> End Type
>
> '==================================================
> ' Private API Declarations
> '==================================================
> Private Declare Sub CopyMem _
> Lib "kernel32" Alias "RtlMoveMemory" ( _
> pTo As Any, _
> pFrom As Any, _
> ByVal nCount As Long)
>
> Private Declare Function lstrlenW _
> Lib "kernel32" ( _
> ByVal pString As Long) As Long
>
> Private Declare Function NetApiBufferFree _
> Lib "netapi32" ( _
> ByVal pBuffer As Long) As Long
>
> Private Declare Function NetUserEnum _
> Lib "netapi32" ( _
> ByVal psServer As Long, _
> ByVal hLevel As Long, _
> ByVal hFilter As Long, _
> pBuffer As Long, _
> ByVal nMaxLength As Long, _
> nEntries As Long, _
> nTotal As Long, _
> hResume As Long) As Long
>
> '==================================================
> ' Private API Constants
> '==================================================
> Private Const ERROR_SUCCESS As Long = 0&
> Private Const ERROR_MORE_DATA As Long = 234&
>
> '==================================================
> ' Private Member Data
> '==================================================
> Private m_usersCollection As Collection
>
> '==================================================
> ' Private Procedures
> '==================================================
>
> '==================================================
> ' Procedure: LoadUsers
> '
> ' Description: This procedure will walk through
> ' the users on a particular machine and add them
> ' onto the collection.
> '==================================================
> Private Sub LoadUsers()
>
> Const MAX_SIZE As Long = 2048&
>
> Dim uUsers() As USER_INFO_0
> Dim hReturn As Long
> Dim pBuffer As Long
> Dim nEntries As Long
> Dim nTotal As Long
> Dim hResume As Long
> Dim iUser As Long
> Dim userEnum As CUser
> Dim sUserID As String
>
> If m_usersCollection Is Nothing Then
> Set m_usersCollection = New Collection
> Do
> If Len(m_sServer) Then
> hReturn = NetUserEnum(StrPtr(m_sServer), 0&, 0&, pBuffer,
> MAX_SIZE, nEntries, nTotal, hResume)
> Else
> hReturn = NetUserEnum(ByVal 0&, 0&, 0&, pBuffer, MAX_SIZE,
> nEntries, nTotal, hResume)
> End If
> If hReturn = ERROR_MORE_DATA Or hReturn = ERROR_SUCCESS Then
> If pBuffer Then
> If nEntries Then
> ReDim uUsers(0 To nEntries - 1) As USER_INFO_0
> CopyMem uUsers(0), ByVal pBuffer, Len(uUsers(0)) *
> nEntries
> For iUser = 0 To nEntries - 1
> Set userEnum = New CUser
> With uUsers(iUser)
> sUserID = WidePointerToString(.psUserName)
> End With
> userEnum.Init sUserID
> m_usersCollection.Add userEnum, sUserID
> Next iUser
> End If
> NetApiBufferFree pBuffer
> pBuffer = 0&
> End If
> End If
> Loop While hReturn = ERROR_MORE_DATA
> End If
>
> End Sub
>
> '==================================================
> ' Procedure: WidePointerToString
> '
> ' Inputs: pString A pointer to a Unicode
> ' string
> '
> ' Returns: String
> '
> ' Description: This procedure will turn a pointer
> ' representation of a string, often from API functions,
> ' and turn it into a string that VB can understand.
> '==================================================
> Private Function WidePointerToString(ByVal pString As Long) As String
>
> Dim sString As String
> Dim nChars As Long
>
> If pString Then
> 'How many characters do I need?
> nChars = lstrlenW(pString)
> If nChars Then
> sString = Space$(nChars)
> 'Multiply the characters by two, to get
> ' the Unicode size in bytes.
> CopyMem ByVal StrPtr(sString), ByVal pString, nChars * 2
> End If
> End If
>
> WidePointerToString = sString
>
> End Function
>
> Ciao, Craig
>
GDavis at 2007-11-11 20:06:55 >

# 4 Re: How can I get users from a domain?
I would like to get this code to work as I have needed this before. In the
code you posted you are missing a variable declaration for 'm_sServer',
I was hoping you could tell me what it is.
"Craig Clearman" <chclear@nospam.please> wrote in message
news:iegscs0bn3jf8bfsskt4tm3utp8gmu76sg@4ax.com...
> Salvino,
>
> >How can i get a list of all NT users in a NT domain?
>
> You're running on NT, I assume? If so, here's the relevant portion of
> a users collection class.
>
> '==================================================
> ' Module: CUsers
> '
> ' Description: This is a collection of the
> ' users defined for a particular machine.
> '
> ' Programmer: Craig Clearman
> '
> ' Revision History:
> ' 30 Apr 96 - Module Created
> '
> '==================================================
>
> '==================================================
> ' Private API Structures
> '==================================================
> Private Type USER_INFO_0
> psUserName As Long
> End Type
>
> '==================================================
> ' Private API Declarations
> '==================================================
> Private Declare Sub CopyMem _
> Lib "kernel32" Alias "RtlMoveMemory" ( _
> pTo As Any, _
> pFrom As Any, _
> ByVal nCount As Long)
>
> Private Declare Function lstrlenW _
> Lib "kernel32" ( _
> ByVal pString As Long) As Long
>
> Private Declare Function NetApiBufferFree _
> Lib "netapi32" ( _
> ByVal pBuffer As Long) As Long
>
> Private Declare Function NetUserEnum _
> Lib "netapi32" ( _
> ByVal psServer As Long, _
> ByVal hLevel As Long, _
> ByVal hFilter As Long, _
> pBuffer As Long, _
> ByVal nMaxLength As Long, _
> nEntries As Long, _
> nTotal As Long, _
> hResume As Long) As Long
>
> '==================================================
> ' Private API Constants
> '==================================================
> Private Const ERROR_SUCCESS As Long = 0&
> Private Const ERROR_MORE_DATA As Long = 234&
>
> '==================================================
> ' Private Member Data
> '==================================================
> Private m_usersCollection As Collection
>
> '==================================================
> ' Private Procedures
> '==================================================
>
> '==================================================
> ' Procedure: LoadUsers
> '
> ' Description: This procedure will walk through
> ' the users on a particular machine and add them
> ' onto the collection.
> '==================================================
> Private Sub LoadUsers()
>
> Const MAX_SIZE As Long = 2048&
>
> Dim uUsers() As USER_INFO_0
> Dim hReturn As Long
> Dim pBuffer As Long
> Dim nEntries As Long
> Dim nTotal As Long
> Dim hResume As Long
> Dim iUser As Long
> Dim userEnum As CUser
> Dim sUserID As String
>
> If m_usersCollection Is Nothing Then
> Set m_usersCollection = New Collection
> Do
> If Len(m_sServer) Then
> hReturn = NetUserEnum(StrPtr(m_sServer), 0&, 0&, pBuffer,
> MAX_SIZE, nEntries, nTotal, hResume)
> Else
> hReturn = NetUserEnum(ByVal 0&, 0&, 0&, pBuffer, MAX_SIZE,
> nEntries, nTotal, hResume)
> End If
> If hReturn = ERROR_MORE_DATA Or hReturn = ERROR_SUCCESS Then
> If pBuffer Then
> If nEntries Then
> ReDim uUsers(0 To nEntries - 1) As USER_INFO_0
> CopyMem uUsers(0), ByVal pBuffer, Len(uUsers(0)) *
> nEntries
> For iUser = 0 To nEntries - 1
> Set userEnum = New CUser
> With uUsers(iUser)
> sUserID = WidePointerToString(.psUserName)
> End With
> userEnum.Init sUserID
> m_usersCollection.Add userEnum, sUserID
> Next iUser
> End If
> NetApiBufferFree pBuffer
> pBuffer = 0&
> End If
> End If
> Loop While hReturn = ERROR_MORE_DATA
> End If
>
> End Sub
>
> '==================================================
> ' Procedure: WidePointerToString
> '
> ' Inputs: pString A pointer to a Unicode
> ' string
> '
> ' Returns: String
> '
> ' Description: This procedure will turn a pointer
> ' representation of a string, often from API functions,
> ' and turn it into a string that VB can understand.
> '==================================================
> Private Function WidePointerToString(ByVal pString As Long) As String
>
> Dim sString As String
> Dim nChars As Long
>
> If pString Then
> 'How many characters do I need?
> nChars = lstrlenW(pString)
> If nChars Then
> sString = Space$(nChars)
> 'Multiply the characters by two, to get
> ' the Unicode size in bytes.
> CopyMem ByVal StrPtr(sString), ByVal pString, nChars * 2
> End If
> End If
>
> WidePointerToString = sString
>
> End Function
>
> Ciao, Craig
>
GDavis at 2007-11-11 20:07:54 >

# 5 Re: How can I get users from a domain?
GDavis
>I would like to get this code to work as I have needed this before. In the
>code you posted you are missing a variable declaration for 'm_sServer',
Must have stripped that out
Private m_sServer As String
It's the server name.
Ciao, Craig
# 6 Re: How can I get users from a domain?
GDavis
>I would like to get this code to work as I have needed this before. In the
>code you posted you are missing a variable declaration for 'm_sServer',
Must have stripped that out
Private m_sServer As String
It's the server name.
Ciao, Craig
# 7 Re: How can I get users from a domain?
I'm now functional, Thanks for the code.
"Craig Clearman" <chclear@nospam.please> wrote in message
news:nbnscs4i0djj9k2s4179q2um48pk6rcvr6@4ax.com...
> GDavis
>
> >I would like to get this code to work as I have needed this before. In
the
> >code you posted you are missing a variable declaration for 'm_sServer',
>
> Must have stripped that out
>
> Private m_sServer As String
>
> It's the server name.
>
> Ciao, Craig
>
GDavis at 2007-11-11 20:10:55 >

# 8 Re: How can I get users from a domain?
I'm now functional, Thanks for the code.
"Craig Clearman" <chclear@nospam.please> wrote in message
news:nbnscs4i0djj9k2s4179q2um48pk6rcvr6@4ax.com...
> GDavis
>
> >I would like to get this code to work as I have needed this before. In
the
> >code you posted you are missing a variable declaration for 'm_sServer',
>
> Must have stripped that out
>
> Private m_sServer As String
>
> It's the server name.
>
> Ciao, Craig
>
GDavis at 2007-11-11 20:12:04 >

# 9 Re: How can I get users from a domain?
salvino,
Or, use ADSI (you have to download and install ADSI on Win9x and NT 4.0,
comes with W2K).
--
L.J. Johnson, Slightly Tilted Software
Microsoft MVP (Visual Basic)
LJJohnsn@Flash.Net or LJJohnson@mvps.org
<http://www.flash.net/~ljjohnsn>
Ask The NT Pro at <http://www.inquiry.com>
"salvino" <salvinos@tiscalinet.it> wrote in message
news:38ce3ba3$1@news.dev-archive.com...
>
> How can i get a list of all NT users in a NT domain?
>
> thanks all.
> Salvino
# 10 Re: How can I get users from a domain?
salvino,
Or, use ADSI (you have to download and install ADSI on Win9x and NT 4.0,
comes with W2K).
--
L.J. Johnson, Slightly Tilted Software
Microsoft MVP (Visual Basic)
LJJohnsn@Flash.Net or LJJohnson@mvps.org
<http://www.flash.net/~ljjohnsn>
Ask The NT Pro at <http://www.inquiry.com>
"salvino" <salvinos@tiscalinet.it> wrote in message
news:38ce3ba3$1@news.dev-archive.com...
>
> How can i get a list of all NT users in a NT domain?
>
> thanks all.
> Salvino