# 1 Re: NetUserEnum
Mike,
>Not long ago someone here helped me figuring out on api NetUserEnum which
>enumerates all LoginNames on the network server. Is there a way to find out
>users First Name and Last Name
>enumerate through each user First and Last names using the same api or some
>other api.
You can either change the structure that you are passing into
NetUserEnum into a level 1 structure, or you can call NetUserGetInfo
with your old user name. Here's an example of the latter:
'==================================================
' Module: CUser
'
' Description: This is a class that contains
' all of the information about one user.
'
' Programmer: Craig Clearman
'
' Revision History:
' 30 Apr 96 - Module Created
'
'==================================================
'==================================================
' Private API Structures
'==================================================
Private Type USER_INFO_1
psName As Long
psPassword As Long
nPasswordAge As Long
hPrivilege As UserPrivileges
psHomeDirectory As Long
psComment As Long
hFlags As UserFlags
psScriptPath As Long
End Type
Private Type USER_INFO_3
psName As Long
psPassword As Long
nPasswordAge As Long
hPrivilege As UserPrivileges
psHomeDirectory As Long
psComment As Long
hFlags As UserFlags
psScriptPath As Long
hAuthFlags As Long
psFullName As Long
psUserComment As Long
psParameters As Long
psWorkstations As Long
dtLastLogon As Long
dtLastLogoff As Long
dtAccountExpires As Long
nMaxStorage As Long
nUnitsPerWeek As Long
pLogonHours As Long
nBadPasswordCount As Long
nLogons As Long
psLogonServer As Long
hCountryCode As Long
hCodePage As Long
hUserID As Long
hGroupID As Long
psProfile As Long
psHomeDirectoryDrive As Long
bPasswordExpired 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 NetUserGetInfo _
Lib "netapi32" ( _
ByVal psServer As Long, _
ByVal psUserID As Long, _
ByVal hLevel As Long, _
pBuffer As Long) As Long
'==================================================
' Private API Constants
'==================================================
Private Const NERR_Success As Long = 0&
'==================================================
' Private Enumerations
'==================================================
'==================================================
' Private Constants
'==================================================
'==================================================
' Private Member Data
'==================================================
'Key Information
Private m_sServer As String
Private m_sUserID As String
'Primary Information
Private m_bRetrievedPrimaryData As Boolean
Private m_sFullName As String
Private m_sPassword As String
Private m_nPasswordAge As Long
Private m_hPrivilege As UserPrivileges
Private m_sHomeDirectory As String
Private m_sDesc As String
Private m_hFlags As UserFlags
Private m_sScriptPath As String
'Level 3 Information
Private m_bRetrievedLevel3Data As Boolean
Private m_bPasswordExpired As Boolean
Private m_bPasswordExpires As Boolean
'==================================================
' Public Enumerations
'==================================================
Public Enum UserFlags
UF_SCRIPT = &H1&
UF_ACCOUNTDISABLE = &H2&
UF_HOMEDIR_REQUIRED = &H8&
UF_LOCKOUT = &H10&
UF_PASSWD_NOTREQD = &H20&
UF_PASSWD_CANT_CHANGE = &H40&
UF_DONT_EXPIRE_PASSWD = &H10000
End Enum
Public Enum UserPrivileges
USER_PRIV_GUEST = 0&
USER_PRIV_USER = 1&
USER_PRIV_ADMIN = 2&
End Enum
'==================================================
' Public Procedures
'==================================================
'==================================================
' Procedure: Description
'
' Description: This procedure will return the
' description for the user.
'==================================================
Public Property Get Description() As String
GetPrimaryData
Description = m_sDesc
End Property
Public Property Let Description(ByVal sDesc As String)
GetPrimaryData
m_sDesc = sDesc
End Property
'==================================================
' Procedure: FullName
'
' Description: This procedure will return the
' full name for the user.
'==================================================
Public Property Get FullName() As String
GetPrimaryData
FullName = m_sFullName
End Property
Public Property Let FullName(ByVal sFullName As String)
GetPrimaryData
m_sFullName = sFullName
End Property
'==================================================
' Procedure: Init
'
' Description: This procedure will initialize
' the state of the user. All required fields will
' be passed here.
'==================================================
Public Sub Init(ByVal sUserID As String)
m_sUserID = sUserID
End Sub
'==================================================
' Procedure: UserID
'
' Description: This procedure will determine
' the logon name that identifies the user.
'==================================================
Public Property Get UserID() As String
UserID = m_sUserID
End Property
Public Property Let UserID(ByVal sUserID As String)
m_sUserID = sUserID
End Property
'==================================================
' Friend Procedures
'==================================================
'==================================================
' Private Procedures
'==================================================
'==================================================
' Procedure: GetLevel3Data
'
' Description: This procedure will get the
' level 3 data for the user. This level contains
' all of the primary data, plus password expiration.
'==================================================
Private Sub GetLevel3Data()
Dim pBuffer As Long
Dim hReturn As Long
Dim uUser As USER_INFO_3
If m_bRetrievedLevel3Data = False Then
hReturn = NetUserGetInfo(0&, StrPtr(m_sUserID), 3&, pBuffer)
If hReturn = NERR_Success Then
CopyMem uUser, ByVal pBuffer, Len(uUser)
With uUser
m_bPasswordExpired = CBool(.bPasswordExpired)
m_bPasswordExpires = Not CBool(.hFlags And
UF_DONT_EXPIRE_PASSWD)
End With
m_bRetrievedLevel3Data = True
End If
If pBuffer Then
NetApiBufferFree pBuffer
End If
End If
End Sub
'==================================================
' Procedure: GetPrimaryData
'
' Description: This procedure will get the
' most used (and most accessible) information
' on the current user.
'==================================================
Private Sub GetPrimaryData()
Dim pBuffer As Long
Dim hReturn As Long
Dim uUser As USER_INFO_1
If m_bRetrievedPrimaryData = False Then
If Len(m_sServer) Then
hReturn = NetUserGetInfo(StrPtr(m_sServer), StrPtr(m_sUserID),
1&, pBuffer)
Else
hReturn = NetUserGetInfo(0&, StrPtr(m_sUserID), 1&, pBuffer)
End If
If hReturn = NERR_Success Then
CopyMem uUser, ByVal pBuffer, Len(uUser)
With uUser
m_sFullName = WidePointerToString(.psName)
m_sPassword = WidePointerToString(.psPassword)
m_nPasswordAge = .nPasswordAge
m_hPrivilege = .hPrivilege
m_sHomeDirectory = WidePointerToString(.psHomeDirectory)
m_sDesc = WidePointerToString(.psComment)
m_hFlags = .hFlags
m_sScriptPath = WidePointerToString(.psScriptPath)
End With
m_bRetrievedPrimaryData = True
End If
If pBuffer Then
NetApiBufferFree pBuffer
End If
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: NetUserEnum
Mike,
>Not long ago someone here helped me figuring out on api NetUserEnum which
>enumerates all LoginNames on the network server. Is there a way to find out
>users First Name and Last Name
>enumerate through each user First and Last names using the same api or some
>other api.
You can either change the structure that you are passing into
NetUserEnum into a level 1 structure, or you can call NetUserGetInfo
with your old user name. Here's an example of the latter:
'==================================================
' Module: CUser
'
' Description: This is a class that contains
' all of the information about one user.
'
' Programmer: Craig Clearman
'
' Revision History:
' 30 Apr 96 - Module Created
'
'==================================================
'==================================================
' Private API Structures
'==================================================
Private Type USER_INFO_1
psName As Long
psPassword As Long
nPasswordAge As Long
hPrivilege As UserPrivileges
psHomeDirectory As Long
psComment As Long
hFlags As UserFlags
psScriptPath As Long
End Type
Private Type USER_INFO_3
psName As Long
psPassword As Long
nPasswordAge As Long
hPrivilege As UserPrivileges
psHomeDirectory As Long
psComment As Long
hFlags As UserFlags
psScriptPath As Long
hAuthFlags As Long
psFullName As Long
psUserComment As Long
psParameters As Long
psWorkstations As Long
dtLastLogon As Long
dtLastLogoff As Long
dtAccountExpires As Long
nMaxStorage As Long
nUnitsPerWeek As Long
pLogonHours As Long
nBadPasswordCount As Long
nLogons As Long
psLogonServer As Long
hCountryCode As Long
hCodePage As Long
hUserID As Long
hGroupID As Long
psProfile As Long
psHomeDirectoryDrive As Long
bPasswordExpired 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 NetUserGetInfo _
Lib "netapi32" ( _
ByVal psServer As Long, _
ByVal psUserID As Long, _
ByVal hLevel As Long, _
pBuffer As Long) As Long
'==================================================
' Private API Constants
'==================================================
Private Const NERR_Success As Long = 0&
'==================================================
' Private Enumerations
'==================================================
'==================================================
' Private Constants
'==================================================
'==================================================
' Private Member Data
'==================================================
'Key Information
Private m_sServer As String
Private m_sUserID As String
'Primary Information
Private m_bRetrievedPrimaryData As Boolean
Private m_sFullName As String
Private m_sPassword As String
Private m_nPasswordAge As Long
Private m_hPrivilege As UserPrivileges
Private m_sHomeDirectory As String
Private m_sDesc As String
Private m_hFlags As UserFlags
Private m_sScriptPath As String
'Level 3 Information
Private m_bRetrievedLevel3Data As Boolean
Private m_bPasswordExpired As Boolean
Private m_bPasswordExpires As Boolean
'==================================================
' Public Enumerations
'==================================================
Public Enum UserFlags
UF_SCRIPT = &H1&
UF_ACCOUNTDISABLE = &H2&
UF_HOMEDIR_REQUIRED = &H8&
UF_LOCKOUT = &H10&
UF_PASSWD_NOTREQD = &H20&
UF_PASSWD_CANT_CHANGE = &H40&
UF_DONT_EXPIRE_PASSWD = &H10000
End Enum
Public Enum UserPrivileges
USER_PRIV_GUEST = 0&
USER_PRIV_USER = 1&
USER_PRIV_ADMIN = 2&
End Enum
'==================================================
' Public Procedures
'==================================================
'==================================================
' Procedure: Description
'
' Description: This procedure will return the
' description for the user.
'==================================================
Public Property Get Description() As String
GetPrimaryData
Description = m_sDesc
End Property
Public Property Let Description(ByVal sDesc As String)
GetPrimaryData
m_sDesc = sDesc
End Property
'==================================================
' Procedure: FullName
'
' Description: This procedure will return the
' full name for the user.
'==================================================
Public Property Get FullName() As String
GetPrimaryData
FullName = m_sFullName
End Property
Public Property Let FullName(ByVal sFullName As String)
GetPrimaryData
m_sFullName = sFullName
End Property
'==================================================
' Procedure: Init
'
' Description: This procedure will initialize
' the state of the user. All required fields will
' be passed here.
'==================================================
Public Sub Init(ByVal sUserID As String)
m_sUserID = sUserID
End Sub
'==================================================
' Procedure: UserID
'
' Description: This procedure will determine
' the logon name that identifies the user.
'==================================================
Public Property Get UserID() As String
UserID = m_sUserID
End Property
Public Property Let UserID(ByVal sUserID As String)
m_sUserID = sUserID
End Property
'==================================================
' Friend Procedures
'==================================================
'==================================================
' Private Procedures
'==================================================
'==================================================
' Procedure: GetLevel3Data
'
' Description: This procedure will get the
' level 3 data for the user. This level contains
' all of the primary data, plus password expiration.
'==================================================
Private Sub GetLevel3Data()
Dim pBuffer As Long
Dim hReturn As Long
Dim uUser As USER_INFO_3
If m_bRetrievedLevel3Data = False Then
hReturn = NetUserGetInfo(0&, StrPtr(m_sUserID), 3&, pBuffer)
If hReturn = NERR_Success Then
CopyMem uUser, ByVal pBuffer, Len(uUser)
With uUser
m_bPasswordExpired = CBool(.bPasswordExpired)
m_bPasswordExpires = Not CBool(.hFlags And
UF_DONT_EXPIRE_PASSWD)
End With
m_bRetrievedLevel3Data = True
End If
If pBuffer Then
NetApiBufferFree pBuffer
End If
End If
End Sub
'==================================================
' Procedure: GetPrimaryData
'
' Description: This procedure will get the
' most used (and most accessible) information
' on the current user.
'==================================================
Private Sub GetPrimaryData()
Dim pBuffer As Long
Dim hReturn As Long
Dim uUser As USER_INFO_1
If m_bRetrievedPrimaryData = False Then
If Len(m_sServer) Then
hReturn = NetUserGetInfo(StrPtr(m_sServer), StrPtr(m_sUserID),
1&, pBuffer)
Else
hReturn = NetUserGetInfo(0&, StrPtr(m_sUserID), 1&, pBuffer)
End If
If hReturn = NERR_Success Then
CopyMem uUser, ByVal pBuffer, Len(uUser)
With uUser
m_sFullName = WidePointerToString(.psName)
m_sPassword = WidePointerToString(.psPassword)
m_nPasswordAge = .nPasswordAge
m_hPrivilege = .hPrivilege
m_sHomeDirectory = WidePointerToString(.psHomeDirectory)
m_sDesc = WidePointerToString(.psComment)
m_hFlags = .hFlags
m_sScriptPath = WidePointerToString(.psScriptPath)
End With
m_bRetrievedPrimaryData = True
End If
If pBuffer Then
NetApiBufferFree pBuffer
End If
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