Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

" How to get user name & domain?

How to retrive username and Domain, ex. 'Domain\user' or 'Workstation\user',
if user logged in local.
[125 byte] By [Michael] at [2007-11-10 0:19:57]
# 1 Re: " How to get user name & domain?
"Michael" <michael@au.ru> wrote:
>How to retrive username and Domain, ex. 'Domain\user' or 'Workstation\user',
>if user logged in local.
>

-------------------
Some one helped me not a long time ago to enumarate through each user on
the netwok server and get login names on this discussion board. I don't know
if it will help you. You can check MSDN on this api NetUserEnum, this api
has different parameter types
USER_INFO_0, USER_INFO_3,USER_INFO_10 etc..... maybe somewhere you can get
the domain name for each user.

Option Explicit

Private Type USER_INFO_0
usri0_name As Long
End Type

Private Declare Function apiNetUserEnum _
Lib "netapi32.DLL" Alias "NetUserEnum" _
(ByVal servername As Long, _
ByVal level As Long, _
ByVal filter As Long, _
bufptr As Long, _
ByVal prefmaxlen As Long, _
entriesread As Long, _
totalentries As Long, _
resume_handle As Long) _
As Long

Private Declare Function apiNetAPIBufferFree _
Lib "netapi32.DLL" Alias "NetApiBufferFree" _
(ByVal buffer As Long) _
As Long

Private Declare Function apilstrlenW _
Lib "kernel32" Alias "lstrlenW" _
(ByVal lpString As Long) _
As Long

Private Declare Sub sapiCopyMem _
Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)

Private Const FILTER_TEMP_DUPLICATE_ACCOUNT = &H1&
Private Const FILTER_NORMAL_ACCOUNT = &H2&
Private Const FILTER_INTERDOMAIN_TRUST_ACCOUNT = &H8&
Private Const FILTER_WORKSTATION_TRUST_ACCOUNT = &H10&
Private Const FILTER_SERVER_TRUST_ACCOUNT = &H20&
Private Const MAX_PREFERRED_LENGTH = -1&

Private Const NERR_SUCCESS = 0
Private Const ERROR_MORE_DATA = 234&
Private Const NERR_BASE = 2100
Private Const NERR_InvalidComputer = (NERR_BASE + 251)
Private Const ERROR_BAD_NETPATH = 53&
Private Const ERROR_INVALID_LEVEL = 124&

Private ADOCon As New ADODB.Connection
Private ADOComm As New ADODB.Command

Function fEnumDomainUsers( _
ByVal strServerName As String) _
As Boolean
' if strServerName=vbNullString,
' assume local machine
' This code only enums global accounts
'
On Error GoTo ErrHandler

Dim abytServerName() As Byte
Dim pBuf As Long
Dim pTmpBuf As USER_INFO_0
Dim dwLevel As Long
Dim dwPrefMaxLen As Long
Dim dwEntriesRead As Long
Dim dwTotalEntries As Long
Dim dwResumeHandle As Long
Dim i As Long
Dim dwTotalCount As Long
Dim nStatus As Long
Dim szName As String

Dim iRecordsAffected As Integer

' assume MAX_PREFERRED_LENGTH
dwPrefMaxLen = MAX_PREFERRED_LENGTH
abytServerName = strServerName & vbNullChar
dwLevel = 0
Do
'only global users
nStatus = apiNetUserEnum(VarPtr(abytServerName(0)), _
dwLevel, _
FILTER_NORMAL_ACCOUNT, _
pBuf, _
dwPrefMaxLen, _
dwEntriesRead, _
dwTotalEntries, _
dwResumeHandle)
'// If the call succeeds,
If ((nStatus = NERR_SUCCESS) Or (nStatus = ERROR_MORE_DATA)) Then
'// Loop through the entries.
For i = 0 To dwEntriesRead - 1
Call sapiCopyMem(pTmpBuf, ByVal (pBuf + (i * 4)), Len(pTmpBuf))
'// Print the name of the user account.
szName = String$(apilstrlenW(pTmpBuf.usri0_name) * 2, vbNullChar)
Call sapiCopyMem(ByVal szName, ByVal pTmpBuf.usri0_name, Len(szName))
Debug.Print StrConv(szName, vbFromUnicode)

szName = StrConv(szName, vbFromUnicode)

dwTotalCount = dwTotalCount + 1
Next
End If

MsgBox dwTotalCount

If (nStatus = NERR_InvalidComputer) Then
'Invalid computer
fEnumDomainUsers = False
MsgBox "Invalid computer"
Exit Function
End If

If (nStatus = ERROR_BAD_NETPATH) Then
'Invalid computer
fEnumDomainUsers = False
MsgBox "BAD NETPATH"
Exit Function

End If
Call apiNetAPIBufferFree(pBuf)
pBuf = 0
Loop While (nStatus = ERROR_MORE_DATA)
If Not (pBuf = 0) Then Call apiNetAPIBufferFree(pBuf)

fEnumDomainUsers = True
ExitHere:
Exit Function
ErrHandler:
fEnumDomainUsers = False
Resume ExitHere
End Function

Private Sub Form_Load()
EstablishConnection

'As a parameter you have to specify a Server name
'If this parameter is Null(Empty String) then Default will be
'your workstation's name.
'Example fEnumDomainUsers ("\\Servername)
fEnumDomainUsers ("\\Servername")

End Sub

-------------------

or you can simply get the current user's id :

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal
lpBuffer As String, nSize As Long) As Long

Public Function GetUserLogin() As String
Dim lSize As Long
Dim iNullPos As Integer
Dim gsLoginName as String

lSize = 49
gsLoginName = String(50, "d")

GetUserName gsLoginName, lSize

gsLoginName = Left(gsLoginName, lSize - 1)

GetUserLogin = gsLoginName

End Function
Mike at 2007-11-11 20:04:22 >
# 2 Re: " How to get user name & domain?
"Michael" <michael@au.ru> wrote:
>How to retrive username and Domain, ex. 'Domain\user' or 'Workstation\user',
>if user logged in local.
>

-------------------
Some one helped me not a long time ago to enumarate through each user on
the netwok server and get login names on this discussion board. I don't know
if it will help you. You can check MSDN on this api NetUserEnum, this api
has different parameter types
USER_INFO_0, USER_INFO_3,USER_INFO_10 etc..... maybe somewhere you can get
the domain name for each user.

Option Explicit

Private Type USER_INFO_0
usri0_name As Long
End Type

Private Declare Function apiNetUserEnum _
Lib "netapi32.DLL" Alias "NetUserEnum" _
(ByVal servername As Long, _
ByVal level As Long, _
ByVal filter As Long, _
bufptr As Long, _
ByVal prefmaxlen As Long, _
entriesread As Long, _
totalentries As Long, _
resume_handle As Long) _
As Long

Private Declare Function apiNetAPIBufferFree _
Lib "netapi32.DLL" Alias "NetApiBufferFree" _
(ByVal buffer As Long) _
As Long

Private Declare Function apilstrlenW _
Lib "kernel32" Alias "lstrlenW" _
(ByVal lpString As Long) _
As Long

Private Declare Sub sapiCopyMem _
Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)

Private Const FILTER_TEMP_DUPLICATE_ACCOUNT = &H1&
Private Const FILTER_NORMAL_ACCOUNT = &H2&
Private Const FILTER_INTERDOMAIN_TRUST_ACCOUNT = &H8&
Private Const FILTER_WORKSTATION_TRUST_ACCOUNT = &H10&
Private Const FILTER_SERVER_TRUST_ACCOUNT = &H20&
Private Const MAX_PREFERRED_LENGTH = -1&

Private Const NERR_SUCCESS = 0
Private Const ERROR_MORE_DATA = 234&
Private Const NERR_BASE = 2100
Private Const NERR_InvalidComputer = (NERR_BASE + 251)
Private Const ERROR_BAD_NETPATH = 53&
Private Const ERROR_INVALID_LEVEL = 124&

Private ADOCon As New ADODB.Connection
Private ADOComm As New ADODB.Command

Function fEnumDomainUsers( _
ByVal strServerName As String) _
As Boolean
' if strServerName=vbNullString,
' assume local machine
' This code only enums global accounts
'
On Error GoTo ErrHandler

Dim abytServerName() As Byte
Dim pBuf As Long
Dim pTmpBuf As USER_INFO_0
Dim dwLevel As Long
Dim dwPrefMaxLen As Long
Dim dwEntriesRead As Long
Dim dwTotalEntries As Long
Dim dwResumeHandle As Long
Dim i As Long
Dim dwTotalCount As Long
Dim nStatus As Long
Dim szName As String

Dim iRecordsAffected As Integer

' assume MAX_PREFERRED_LENGTH
dwPrefMaxLen = MAX_PREFERRED_LENGTH
abytServerName = strServerName & vbNullChar
dwLevel = 0
Do
'only global users
nStatus = apiNetUserEnum(VarPtr(abytServerName(0)), _
dwLevel, _
FILTER_NORMAL_ACCOUNT, _
pBuf, _
dwPrefMaxLen, _
dwEntriesRead, _
dwTotalEntries, _
dwResumeHandle)
'// If the call succeeds,
If ((nStatus = NERR_SUCCESS) Or (nStatus = ERROR_MORE_DATA)) Then
'// Loop through the entries.
For i = 0 To dwEntriesRead - 1
Call sapiCopyMem(pTmpBuf, ByVal (pBuf + (i * 4)), Len(pTmpBuf))
'// Print the name of the user account.
szName = String$(apilstrlenW(pTmpBuf.usri0_name) * 2, vbNullChar)
Call sapiCopyMem(ByVal szName, ByVal pTmpBuf.usri0_name, Len(szName))
Debug.Print StrConv(szName, vbFromUnicode)

szName = StrConv(szName, vbFromUnicode)

dwTotalCount = dwTotalCount + 1
Next
End If

MsgBox dwTotalCount

If (nStatus = NERR_InvalidComputer) Then
'Invalid computer
fEnumDomainUsers = False
MsgBox "Invalid computer"
Exit Function
End If

If (nStatus = ERROR_BAD_NETPATH) Then
'Invalid computer
fEnumDomainUsers = False
MsgBox "BAD NETPATH"
Exit Function

End If
Call apiNetAPIBufferFree(pBuf)
pBuf = 0
Loop While (nStatus = ERROR_MORE_DATA)
If Not (pBuf = 0) Then Call apiNetAPIBufferFree(pBuf)

fEnumDomainUsers = True
ExitHere:
Exit Function
ErrHandler:
fEnumDomainUsers = False
Resume ExitHere
End Function

Private Sub Form_Load()
EstablishConnection

'As a parameter you have to specify a Server name
'If this parameter is Null(Empty String) then Default will be
'your workstation's name.
'Example fEnumDomainUsers ("\\Servername)
fEnumDomainUsers ("\\Servername")

End Sub

-------------------

or you can simply get the current user's id :

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal
lpBuffer As String, nSize As Long) As Long

Public Function GetUserLogin() As String
Dim lSize As Long
Dim iNullPos As Integer
Dim gsLoginName as String

lSize = 49
gsLoginName = String(50, "d")

GetUserName gsLoginName, lSize

gsLoginName = Left(gsLoginName, lSize - 1)

GetUserLogin = gsLoginName

End Function
Mike at 2007-11-11 20:05:27 >
# 3 Re: " How to get user name & domain?
This sample retrive only user name but not Domain when user logged in.
For ex. in network 2 domains: DOMAIN1 and DOMAIN2 and if both domain exists
user name 'User'.
How in this ex. retrive the domain?

Mike wrote in message <38d0f5d3$1@news.dev-archive.com>...
>
>"Michael" <michael@au.ru> wrote:
>>How to retrive username and Domain, ex. 'Domain\user' or
'Workstation\user',
>>if user logged in local.
>>
>
>
>-------------------
>Some one helped me not a long time ago to enumarate through each user on
>the netwok server and get login names on this discussion board. I don't
know
>if it will help you. You can check MSDN on this api NetUserEnum, this api
>has different parameter types
>USER_INFO_0, USER_INFO_3,USER_INFO_10 etc..... maybe somewhere you can get
>the domain name for each user.
>
>
>
>Option Explicit
>
>Private Type USER_INFO_0
> usri0_name As Long
>End Type
>
>Private Declare Function apiNetUserEnum _
> Lib "netapi32.DLL" Alias "NetUserEnum" _
> (ByVal servername As Long, _
> ByVal level As Long, _
> ByVal filter As Long, _
> bufptr As Long, _
> ByVal prefmaxlen As Long, _
> entriesread As Long, _
> totalentries As Long, _
> resume_handle As Long) _
> As Long
>
>Private Declare Function apiNetAPIBufferFree _
> Lib "netapi32.DLL" Alias "NetApiBufferFree" _
> (ByVal buffer As Long) _
> As Long
>
>Private Declare Function apilstrlenW _
> Lib "kernel32" Alias "lstrlenW" _
> (ByVal lpString As Long) _
> As Long
>
>Private Declare Sub sapiCopyMem _
> Lib "kernel32" Alias "RtlMoveMemory" _
> (Destination As Any, _
> Source As Any, _
> ByVal Length As Long)
>
>Private Const FILTER_TEMP_DUPLICATE_ACCOUNT = &H1&
>Private Const FILTER_NORMAL_ACCOUNT = &H2&
>Private Const FILTER_INTERDOMAIN_TRUST_ACCOUNT = &H8&
>Private Const FILTER_WORKSTATION_TRUST_ACCOUNT = &H10&
>Private Const FILTER_SERVER_TRUST_ACCOUNT = &H20&
>Private Const MAX_PREFERRED_LENGTH = -1&
>
>Private Const NERR_SUCCESS = 0
>Private Const ERROR_MORE_DATA = 234&
>Private Const NERR_BASE = 2100
>Private Const NERR_InvalidComputer = (NERR_BASE + 251)
>Private Const ERROR_BAD_NETPATH = 53&
>Private Const ERROR_INVALID_LEVEL = 124&
>
>Private ADOCon As New ADODB.Connection
>Private ADOComm As New ADODB.Command
>
>Function fEnumDomainUsers( _
> ByVal strServerName As String) _
> As Boolean
>' if strServerName=vbNullString,
>' assume local machine
>' This code only enums global accounts
>'
>On Error GoTo ErrHandler
>
>Dim abytServerName() As Byte
>Dim pBuf As Long
>Dim pTmpBuf As USER_INFO_0
>Dim dwLevel As Long
>Dim dwPrefMaxLen As Long
>Dim dwEntriesRead As Long
>Dim dwTotalEntries As Long
>Dim dwResumeHandle As Long
>Dim i As Long
>Dim dwTotalCount As Long
>Dim nStatus As Long
>Dim szName As String
>
>Dim iRecordsAffected As Integer
>
> ' assume MAX_PREFERRED_LENGTH
> dwPrefMaxLen = MAX_PREFERRED_LENGTH
> abytServerName = strServerName & vbNullChar
> dwLevel = 0
> Do
> 'only global users
> nStatus = apiNetUserEnum(VarPtr(abytServerName(0)), _
> dwLevel, _
> FILTER_NORMAL_ACCOUNT, _
> pBuf, _
> dwPrefMaxLen, _
> dwEntriesRead, _
> dwTotalEntries, _
> dwResumeHandle)
> '// If the call succeeds,
> If ((nStatus = NERR_SUCCESS) Or (nStatus = ERROR_MORE_DATA)) Then
>'// Loop through the entries.
> For i = 0 To dwEntriesRead - 1
> Call sapiCopyMem(pTmpBuf, ByVal (pBuf + (i * 4)),
Len(pTmpBuf))
> '// Print the name of the user account.
> szName = String$(apilstrlenW(pTmpBuf.usri0_name) * 2,
vbNullChar)
> Call sapiCopyMem(ByVal szName, ByVal pTmpBuf.usri0_name,
Len(szName))
> Debug.Print StrConv(szName, vbFromUnicode)
>
> szName = StrConv(szName, vbFromUnicode)
>
> dwTotalCount = dwTotalCount + 1
> Next
> End If
>
> MsgBox dwTotalCount
>
> If (nStatus = NERR_InvalidComputer) Then
> 'Invalid computer
> fEnumDomainUsers = False
> MsgBox "Invalid computer"
> Exit Function
> End If
>
>
> If (nStatus = ERROR_BAD_NETPATH) Then
> 'Invalid computer
> fEnumDomainUsers = False
> MsgBox "BAD NETPATH"
> Exit Function
>
> End If
> Call apiNetAPIBufferFree(pBuf)
> pBuf = 0
> Loop While (nStatus = ERROR_MORE_DATA)
> If Not (pBuf = 0) Then Call apiNetAPIBufferFree(pBuf)
>
> fEnumDomainUsers = True
>ExitHere:
> Exit Function
>ErrHandler:
> fEnumDomainUsers = False
> Resume ExitHere
>End Function
>
>Private Sub Form_Load()
>EstablishConnection
>
>'As a parameter you have to specify a Server name
>'If this parameter is Null(Empty String) then Default will be
>'your workstation's name.
>'Example fEnumDomainUsers ("\\Servername)
> fEnumDomainUsers ("\\Servername")
>
>End Sub
>
>-------------------
>
>or you can simply get the current user's id :
>
>
>
>Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal
>lpBuffer As String, nSize As Long) As Long
>
>Public Function GetUserLogin() As String
> Dim lSize As Long
> Dim iNullPos As Integer
> Dim gsLoginName as String
>
> lSize = 49
> gsLoginName = String(50, "d")
>
>
> GetUserName gsLoginName, lSize
>
> gsLoginName = Left(gsLoginName, lSize - 1)
>
> GetUserLogin = gsLoginName
>
>End Function
Michael at 2007-11-11 20:06:31 >
# 4 Re: " How to get user name & domain?
This sample retrive only user name but not Domain when user logged in.
For ex. in network 2 domains: DOMAIN1 and DOMAIN2 and if both domain exists
user name 'User'.
How in this ex. retrive the domain?

Mike wrote in message <38d0f5d3$1@news.dev-archive.com>...
>
>"Michael" <michael@au.ru> wrote:
>>How to retrive username and Domain, ex. 'Domain\user' or
'Workstation\user',
>>if user logged in local.
>>
>
>
>-------------------
>Some one helped me not a long time ago to enumarate through each user on
>the netwok server and get login names on this discussion board. I don't
know
>if it will help you. You can check MSDN on this api NetUserEnum, this api
>has different parameter types
>USER_INFO_0, USER_INFO_3,USER_INFO_10 etc..... maybe somewhere you can get
>the domain name for each user.
>
>
>
>Option Explicit
>
>Private Type USER_INFO_0
> usri0_name As Long
>End Type
>
>Private Declare Function apiNetUserEnum _
> Lib "netapi32.DLL" Alias "NetUserEnum" _
> (ByVal servername As Long, _
> ByVal level As Long, _
> ByVal filter As Long, _
> bufptr As Long, _
> ByVal prefmaxlen As Long, _
> entriesread As Long, _
> totalentries As Long, _
> resume_handle As Long) _
> As Long
>
>Private Declare Function apiNetAPIBufferFree _
> Lib "netapi32.DLL" Alias "NetApiBufferFree" _
> (ByVal buffer As Long) _
> As Long
>
>Private Declare Function apilstrlenW _
> Lib "kernel32" Alias "lstrlenW" _
> (ByVal lpString As Long) _
> As Long
>
>Private Declare Sub sapiCopyMem _
> Lib "kernel32" Alias "RtlMoveMemory" _
> (Destination As Any, _
> Source As Any, _
> ByVal Length As Long)
>
>Private Const FILTER_TEMP_DUPLICATE_ACCOUNT = &H1&
>Private Const FILTER_NORMAL_ACCOUNT = &H2&
>Private Const FILTER_INTERDOMAIN_TRUST_ACCOUNT = &H8&
>Private Const FILTER_WORKSTATION_TRUST_ACCOUNT = &H10&
>Private Const FILTER_SERVER_TRUST_ACCOUNT = &H20&
>Private Const MAX_PREFERRED_LENGTH = -1&
>
>Private Const NERR_SUCCESS = 0
>Private Const ERROR_MORE_DATA = 234&
>Private Const NERR_BASE = 2100
>Private Const NERR_InvalidComputer = (NERR_BASE + 251)
>Private Const ERROR_BAD_NETPATH = 53&
>Private Const ERROR_INVALID_LEVEL = 124&
>
>Private ADOCon As New ADODB.Connection
>Private ADOComm As New ADODB.Command
>
>Function fEnumDomainUsers( _
> ByVal strServerName As String) _
> As Boolean
>' if strServerName=vbNullString,
>' assume local machine
>' This code only enums global accounts
>'
>On Error GoTo ErrHandler
>
>Dim abytServerName() As Byte
>Dim pBuf As Long
>Dim pTmpBuf As USER_INFO_0
>Dim dwLevel As Long
>Dim dwPrefMaxLen As Long
>Dim dwEntriesRead As Long
>Dim dwTotalEntries As Long
>Dim dwResumeHandle As Long
>Dim i As Long
>Dim dwTotalCount As Long
>Dim nStatus As Long
>Dim szName As String
>
>Dim iRecordsAffected As Integer
>
> ' assume MAX_PREFERRED_LENGTH
> dwPrefMaxLen = MAX_PREFERRED_LENGTH
> abytServerName = strServerName & vbNullChar
> dwLevel = 0
> Do
> 'only global users
> nStatus = apiNetUserEnum(VarPtr(abytServerName(0)), _
> dwLevel, _
> FILTER_NORMAL_ACCOUNT, _
> pBuf, _
> dwPrefMaxLen, _
> dwEntriesRead, _
> dwTotalEntries, _
> dwResumeHandle)
> '// If the call succeeds,
> If ((nStatus = NERR_SUCCESS) Or (nStatus = ERROR_MORE_DATA)) Then
>'// Loop through the entries.
> For i = 0 To dwEntriesRead - 1
> Call sapiCopyMem(pTmpBuf, ByVal (pBuf + (i * 4)),
Len(pTmpBuf))
> '// Print the name of the user account.
> szName = String$(apilstrlenW(pTmpBuf.usri0_name) * 2,
vbNullChar)
> Call sapiCopyMem(ByVal szName, ByVal pTmpBuf.usri0_name,
Len(szName))
> Debug.Print StrConv(szName, vbFromUnicode)
>
> szName = StrConv(szName, vbFromUnicode)
>
> dwTotalCount = dwTotalCount + 1
> Next
> End If
>
> MsgBox dwTotalCount
>
> If (nStatus = NERR_InvalidComputer) Then
> 'Invalid computer
> fEnumDomainUsers = False
> MsgBox "Invalid computer"
> Exit Function
> End If
>
>
> If (nStatus = ERROR_BAD_NETPATH) Then
> 'Invalid computer
> fEnumDomainUsers = False
> MsgBox "BAD NETPATH"
> Exit Function
>
> End If
> Call apiNetAPIBufferFree(pBuf)
> pBuf = 0
> Loop While (nStatus = ERROR_MORE_DATA)
> If Not (pBuf = 0) Then Call apiNetAPIBufferFree(pBuf)
>
> fEnumDomainUsers = True
>ExitHere:
> Exit Function
>ErrHandler:
> fEnumDomainUsers = False
> Resume ExitHere
>End Function
>
>Private Sub Form_Load()
>EstablishConnection
>
>'As a parameter you have to specify a Server name
>'If this parameter is Null(Empty String) then Default will be
>'your workstation's name.
>'Example fEnumDomainUsers ("\\Servername)
> fEnumDomainUsers ("\\Servername")
>
>End Sub
>
>-------------------
>
>or you can simply get the current user's id :
>
>
>
>Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal
>lpBuffer As String, nSize As Long) As Long
>
>Public Function GetUserLogin() As String
> Dim lSize As Long
> Dim iNullPos As Integer
> Dim gsLoginName as String
>
> lSize = 49
> gsLoginName = String(50, "d")
>
>
> GetUserName gsLoginName, lSize
>
> gsLoginName = Left(gsLoginName, lSize - 1)
>
> GetUserLogin = gsLoginName
>
>End Function
Michael at 2007-11-11 20:07:31 >