GetUserName Function

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

Platforms: Win 95/98, Win NT

GetUserName retrieves the name of the user who is logged on to Windows. This user's saved settings are the ones used during the current Windows session. The name of the user is placed in the string passed as lpBuffer. The function puts the size of the returned string into the variable passed as nSize. The function returns 0 if an error occured, or a non-zero value if successful.

lpBuffer
String which receives the name of the user logged on to Windows. The string passed must have sufficient room to receive the string the function will give it.
nSize
The size of the string passed to the function. The variable passed as this parameter also receives the size of the returned string (including the terminating null character).

Example:

' Display the name of the user currently logged on.
Dim username As String  ' receives name of the user
Dim slength As Long  ' length of the string
Dim retval As Long  ' return value

' Create room in the buffer to receive the returned string.
username = Space(255)  ' room for 255 characters
slength = 255  ' initialize the size of the string
' Get the user's name and display it.
retval = GetUserName(username, slength)  ' slength is now the length of the returned string
username = Left(username, slength - 1)  ' extract the returned info from the buffer
' (We subtracted one because we don't want the null character in the trimmed string.)
Debug.Print "The name of the current user is "; username

Category: System Information

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getusername.html