469,927 Members | 1,737 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Function to return Username (NT Login) of current user

MMcCarthy
14,534 Expert Mod 8TB
This function returns the username of the currently logged in NT User.

Expand|Select|Wrap|Line Numbers
  1. ' This library must be declared
  2. Declare Function GetUserName& _
  3.                      Lib "advapi32.dll" Alias _
  4.                      "GetUserNameA" (ByVal lpBuffer As String,
  5.                                      nSize As Long)
  6.  
  7. Function sys_OrigUserID() As String
  8. Dim s$, cnt&, dl&
  9. Dim max_String As Integer
  10. Dim username As String
  11.  
  12.     max_String = 30
  13.     cnt& = 199
  14.     s$ = String$(max_String, 1)
  15.     dl& = GetUserName(s$, cnt)
  16.     username = Trim$(Left$(s$, cnt))
  17.     username = UCase(Mid(username, 1, Len(username) - 1))
  18.     sys_OrigUserID = username
  19.  
  20. End Function
May 29 '07 #1
4 34470
nico5038
3,080 Expert 2GB
As an alternative you can use the Environ() function like:
Expand|Select|Wrap|Line Numbers
  1. 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)
May 29 '07 #2
NeoPa
32,232 Expert Mod 16PB
.
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().
Nov 20 '07 #3
NeoPa
32,232 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" _
  2.     Alias "GetUserNameA" (ByVal lpBuffer As String, _
  3.                           lpnSize As Long) As Long
  4.  
  5. 'GetLogonName() determines the Account ID of the current user.
  6. Public Function GetLogonName() As String
  7.     Dim lngMax As Long
  8.     Dim strBuffer As String
  9.  
  10.     lngMax = &HFF
  11.     strBuffer = String(lngMax, vbNullChar)
  12.     Call GetUserName(lpBuffer:=strBuffer, lpnSize:=lngMax)
  13.     GetLogonName = Trim(Left(strBuffer, lngMax - 1))
  14. End Function
4 Weeks Ago #4
NeoPa
32,232 Expert Mod 16PB
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.
4 Weeks Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Rudolf Bargholz | last post: by
2 posts views Thread by Jesper Stocholm | last post: by
4 posts views Thread by Leszek | last post: by
reply views Thread by =?Utf-8?B?QW50b25pbyBPJydOZWFs?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.