How to Get current user Name (in three ways)
Posted on August 7, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 'System & API - How to Get current user Name (in three ways) '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. |