by Matthew Grove (2 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (3 Votes)
Returns the current user name using a dll call
Code Returns
The current user that is logged on
API DeclarationsPrivate Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
Function GetUser()
' This function uses a windows dll to query the registry automatically ti return the user name
Dim sBuffer As String
Dim lSize As Long
' Parameters for the dll declaration are set
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize) ' Call the declared dll function
If lSize > 0 Then
GetUser = Left$(sBuffer, lSize) ' Remove empty spaces
Else
GetUser = vbNullString ' Return empty if no user is found
End If
End Function