PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Classic VB/API - How Do I Get The Current Windows Username?-VBForums
Results 1 to 3 of 3

Thread: Classic VB/API - How Do I Get The Current Windows Username?

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,650

    Arrow Classic VB/API - How Do I Get The Current Windows Username?

    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 -
    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.


    To get the username this way is simple:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     MsgBox Environ("USERNAME")
    5. 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).

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" _
    4. (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
    5.  
    6.  
    7. Private Sub Command1_Click()
    8.    
    9.     Dim strUserName As String * 255 'Create a string buffer of 255 chars in length
    10.     Dim x As Integer
    11.  
    12.     x = GetEnvironmentVariable("USERNAME", strUserName, Len(strUserName))
    13.     If x > 0 Then
    14.         'Look for Null Character, usually included
    15.         x = InStr(strUserName, vbNullChar)
    16.         'Trim off buffered spaces too
    17.         If x > 0 Then
    18.             MsgBox (Left$(strUserName, x - 1))
    19.         Else
    20.             MsgBox (Left$(strUserName, x))
    21.         End If
    22.     End If
    23.  
    24. 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.

    VB Code:
    1. 'If placed in a module it will be available to all forms in your project for calling
    2. 'Inside Module1.bas
    3. Option Explicit
    4.  
    5. Private Declare Function GetUserName Lib "advapi32.dll" _
    6. Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    7.  
    8. Public Function CurrentUser() As String
    9. '*********************************************************
    10. '* Function to get the current logged on user in windows *
    11. '*********************************************************
    12.  
    13.     Dim strBuff As String * 255
    14.     Dim X As Long
    15.  
    16.     CurrentUser = ""
    17.     X = GetUserName(strBuff, Len(strBuff) - 1)
    18.     If X > 0 Then
    19.         'Look for Null Character, usually included
    20.         X = InStr(strBuff, vbNullChar)
    21.         'Trim off buffered spaces too
    22.         If X > 0 Then
    23.             CurrentUser = UCase(Left$(strBuff, X - 1)) 'UCase is optional ;)
    24.         Else
    25.             CurrentUser = UCase(Left$(strBuff, X))
    26.         End If
    27.     End If
    28.  
    29. End Function
    30.  
    31.  
    32. 'Sample usage:
    33. 'Behind Form1.frm (or whatever your form's name is)
    34. Option Explicit
    35.  
    36. Private Sub Form_Load()
    37.     MsgBox CurrentUser
    38. 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:
    VB Code:
    1. Option Explicit
    2. Dim sUserName as String
    3.  
    4. Private Sub Form_Load()
    5.     sUserName = CurrentUser
    6. End Sub

    Enjoy


    Gangsta Yoda™ ®


    Last edited by si_the_geek; Aug 29th, 2005 at 09:00 PM.

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Classic VB/API - How Do I Get The Current Windows Username?

    Another way is:
    VB Code:
    1. Private Enum EXTENDED_NAME_FORMAT
    2.     NameUnknown = 0
    3.     NameFullyQualifiedDN = 1
    4.     NameSamCompatible = 2
    5.     NameDisplay = 3
    6.     NameUniqueId = 6
    7.     NameCanonical = 7
    8.     NameUserPrincipal = 8
    9.     NameCanonicalEx = 9
    10.     NameServicePrincipal = 10
    11. End Enum
    12.  
    13. Private Declare Function GetUserNameEx Lib "secur32.dll" Alias _
    14. "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, _
    15. ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
    16.  
    17. Private Sub Command1_Click()
    18. 'KPD-Team 2001
    19. 'URL: [url]http://www.allapi.net/[/url]
    20. 'E-Mail: [email]KPDTeam@allapi.net[/email]
    21. Dim sBuffer As String, Ret As Long
    22. sBuffer = String(256, 0)
    23. Ret = Len(sBuffer)
    24. If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
    25.     MsgBox "Username: " + Left$(sBuffer, Ret)
    26. Else
    27.     MsgBox "Error while retrieving the username"
    28. End If
    29. End Sub
    Please note that this method requires Windows 2000 or later and does work with XP.
    Last edited by si_the_geek; Oct 7th, 2005 at 08:29 AM.

  3. #3
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Classic VB/API - How Do I Get The Current Windows Username?

    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.
    Last edited by si_the_geek; Oct 7th, 2005 at 08:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width