Home > How-To Library > System & API

How to Get current user Name (in three ways)

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
'1. Using the Environment Variable: ' Using the environment variables are easy to use but unreliable for these reasons - ' 1. The user can edit the value to anything they want by going to the ' System Properties and changing the values. ' 2. The user can delete the environment variables too. ' 3. Another thing that is important to note is that this variable is not set ' by default on Win9x/Me. ' ' To get the username this way is simple: Option Explicit Private Sub Command1_Click() MsgBox Environ("USERNAME") End Sub '2. Using the API: ' There are two APIs that can retrieve the user name. ' (i) GetEnvironmentVariable API: ' It is just another way to read an environment variable (and as such, ' has the same problems mentioned above). Option Explicit Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" _ (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Sub Command1_Click() Dim strUserName As String * 255 'Create a string buffer of 255 chars in length Dim X As Integer X = GetEnvironmentVariable("USERNAME", strUserName, Len(strUserName)) If X > 0 Then 'Look for Null Character, usually included X = InStr(strUserName, vbNullChar) 'Trim off buffered spaces too If X > 0 Then MsgBox (Left$(strUserName, X - 1)) Else MsgBox (Left$(strUserName, X)) End If End If End Sub ' (ii) The GetUserName API: ' It is probably the most reliable and secure way to retrieve the username. 'It cannot be changed by the user as long as Windows permissions dissallow it. 'If placed in a module it will be available to all forms in your project for calling 'Inside Module1.bas Option Explicit Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal lpBuffer As String, nSize As Long) As Long Public Function CurrentUser() As String '* Function to get the current logged on user in windows * Dim strBuff As String * 255 Dim X As Long CurrentUser = "" X = GetUserName(strBuff, Len(strBuff) - 1) If X > 0 Then 'Look for Null Character, usually included X = InStr(strBuff, vbNullChar) 'Trim off buffered spaces too If X > 0 Then CurrentUser = UCase(Left$(strBuff, X - 1)) 'UCase is optional Else CurrentUser = UCase(Left$(strBuff, X)) End If End If End Function 'Sample usage: 'Behind Form1.frm (or whatever your form's name is) Option Explicit Private Sub Form_Load() MsgBox CurrentUser End Sub 'Another way is: Private Enum EXTENDED_NAME_FORMAT NameUnknown = 0 NameFullyQualifiedDN = 1 NameSamCompatible = 2 NameDisplay = 3 NameUniqueId = 6 NameCanonical = 7 NameUserPrincipal = 8 NameCanonicalEx = 9 NameServicePrincipal = 10 End Enum Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" _ (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, _ ByRef nSize As Long) As Long Private Sub Command1_Click() Dim sBuffer As String, Ret As Long sBuffer = String(256, 0) Ret = Len(sBuffer) If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then MsgBox "Username: " + Left$(sBuffer, Ret) Else MsgBox "Error while retrieving the username" End If End Sub 'Please note that this method requires Windows 2000 or later and does work with XP.

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.