GetUserName Function - Windows API

Purpose

Returns the username of the current Windows user

Code

VB6

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

Public Function GetUserName()

    Dim result As String
    Dim n As Long
    Dim retval As Long
    
    n = 255
    result = Space$(n)
    retval = GetUserNameApi(result, n)
    GetUserName = Left$(result, n - 1)

End Function