PDA

Click to See Complete Forum and Search --> : Classic VB/API - How Do I Get The Current Windows Username?


RobDog888
Aug 29th, 2005, 04:42 PM
There are a few ways to get the Windows current logged on user name.

1. The Environment Variable:
Using the environment variables are easy to use but unreliable for these reasons - The user can edit the value to anything they want by going to the System Properties and changing the values.
The user can delete the environment variables too.

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.

• The 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


• 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


Note: all of these examples use message boxes to display the user name, but you can put the user name into a variable instead by replacing MsgBox in each of the examples with Variable = (where Variable is the name of a String variable). In the last example, you could use this as the form code to put the user name into the variable sUserName:
Option Explicit
Dim sUserName as String

Private Sub Form_Load()
sUserName = CurrentUser
End Sub

Enjoy :)


Gangsta Yoda™ http://www.vbforums.com/attachment.php?attachmentid=38679®

Hack
Sep 8th, 2005, 02:40 PM
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()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@allapi.net
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.

Joacim Andersson
Oct 6th, 2005, 04:27 AM
Another thing that is important to note about using the environment variable to get the user name is that this variable is not set by default on Win9x/Me.