Function to return Username (NT Login) of current user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    Function to return Username (NT Login) of current user

    This function returns the username of the currently logged in NT User.

    [CODE=vb]
    ' This library must be declared
    Declare Function GetUserName& _
    Lib "advapi32.d ll" Alias _
    "GetUserNam eA" (ByVal lpBuffer As String,
    nSize As Long)

    Function sys_OrigUserID( ) As String
    Dim s$, cnt&, dl&
    Dim max_String As Integer
    Dim username As String

    max_String = 30
    cnt& = 199
    s$ = String$(max_Str ing, 1)
    dl& = GetUserName(s$, cnt)
    username = Trim$(Left$(s$, cnt))
    username = UCase(Mid(usern ame, 1, Len(username) - 1))
    sys_OrigUserID = username

    End Function[/CODE]
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    As an alternative you can use the Environ() function like:
    Code:
    strUser = Environ("username")
    This Environ() function can be used to extract all system variables like the computername, etc. as long as the variable has been defined and has been filled.

    To get all available variables you can open the cmd window (Start / Run.. and enter the string "cmd") now by typing SET after the "C:>" prompt and pressing [Enter] all available variables (including the "username") will show and all of these can be extracted with this Environ() function. Keep however in mind that not all variables will be available on all computers...

    Nic;o)

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32661

      #3
      .
      Bear in mind also that the Environ() function will return whatever is in the Environment Variable "USERNAME". While this is set up with the username, there's nothing to stop it being changed within a session. If you're using this as part of a security feature then it's best to use GetUserName().
      Last edited by NeoPa; Oct 5 '10, 03:27 PM. Reason: Appeared invisible in Firefox :S

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        Code:
        Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" _
            Alias "GetUserNameA" (ByVal lpBuffer As String, _
                                  lpnSize As Long) As Long
        
        'GetLogonName() determines the Account ID of the current user.
        Public Function GetLogonName() As String
            Dim lngMax As Long
            Dim strBuffer As String
        
            lngMax = &HFF
            strBuffer = String(lngMax, vbNullChar)
            Call GetUserName(lpBuffer:=strBuffer, lpnSize:=lngMax)
            GetLogonName = Trim(Left(strBuffer, lngMax - 1))
        End Function

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32661

          #5
          Although Mary's code will still work in 32 bit mode (If #If Win64 resolves to False but NOT if #If Win32 resolves to True as that one resolves to True in both 32 & 64 bit environments.) there are changes necessary to ensure it also works for 64 bit mode.

          Notice this version no longer uses implicit type declaration characters as these are no longer recommended for serious code (Fine for knocking something up quickly to test hypotheses etc). It also makes life harder for you when trying to update code to work in new environments (such as 32 bit to 64 bit). Essentially it's less portable so not advisable.

          This is one example that doesn't need compiler directives such as #If Win64 etc as it works perfectly in both environments.

          In this case it may help to know, for line #12, that while lngMax is passed across as a simple Long value, strBuffer only has its address passed. This is handly as this will be four bytes of address in 32 bit mode but eight bytes in 64 bit. We don't have to worry about that though as both the calling and called code will be set to work the same way by the compiler.
          Last edited by NeoPa; Dec 26 '21, 07:00 AM.

          Comment

          Working...