search

VB6: Geting Windows Username & Domain Information

Recently, i am working on a project to integrate existing old system with Windows Login authentication as the goal is to remove the form username and password. 

As this legacy application is hosted and run on a private network, security concern is not an issue. Therefore i decided just to authenticate users based on program authenticate check with existing database for existence of the Windows AD username in it. 

With that being said, all i need is just a way to retrieve the windows username and domain the user is currently logged to for the client application. 

There are few methods this can be done. 

Using "Environ" Variable

This methods works on Windows NT and up only.

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.

Dim UserName As String
Dim UserDomain As String
UserName   = Environ("USERNAME")
UserDomain = Environ("USERDOMAIN")

MsgBox "My Username is " & UserName & " and my Domain is " UserDomain 

Using API Method

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
 


Tips for Tagging for Youtube Videos
3 Dos Games - Prince of Persia 1 & 2 and Digger
 

ABOUT

An experienced web master with more than 10 years of functional web design experiences and have designed websites and applications since 2006 as a freelance web-designer, mastering coding, deployment, database, designs and more.