Private Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function GetCurrentNTLogin() As String
On Error GoTo ErrGetCurrentNTLogin
Dim lLengthUserName As Long
Dim sUserName As String
Dim lResult As Long
' Use the GetUserName API to find out who is currently logged onto
' this system. Preset the length of the string to hold the
' returned user name from the "GetUserName" API.
lLengthUserName = 255
sUserName = Space(lLengthUserName)
' Call GetUserName to find out who is logged onto this system.
lResult = GetUserName(sUserName, lLengthUserName)
' Return value of zero means the call failed; test for this before
' continuing.
If (lResult = 0) Then
sUserName = Environ("USERNAME"
End If
GetCurrentNTLogin = TrimBuffer(sUserName)
Exit Function
'*** Error Trap ***
ErrGetCurrentNTLogin:
Err.Raise Err.Number, "GetCurrentNTLogin:" & MODULE_NAME & vbCrLf & Err.Source, Err.Description
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.