473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,388 developers and data experts.

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 36171
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,556 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,556 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
Dec 26 '21 #4
NeoPa
32,556 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.
Dec 26 '21 #5

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

Similar topics

1
by: Rudolf Bargholz | last post by:
Hi, DB2 v7.1 FP3 on Windows 2003 I am trying to acheive the following: create trigger ADD_LHL after insert on MAINTABLE for each row mode db2sql insert into LOGTABLE
2
by: Jesper Stocholm | last post by:
I have implemented role-based security within my ASP.Net application. However, it seems the role is not passed to the authentication ticket I create. I want to use it to display/hide some...
0
by: Giovanni Bassi | last post by:
Hello Group, I using impersonation on my web app. I am explicitely setting the User with the identity element in the web.config like this: <identity impersonate="true" userName="MyUsr"...
4
by: Joe | last post by:
This may actually be an IIS configuration issue but any help would be appreciated. I'd like to display some content based upon who the current user is that is accessing an internal ASP.NET...
4
by: Leszek | last post by:
Hello! I have my webpage (ASP) in domain (IIS 6.0). I need to make a popup with fullname of current user. How can I do this? Maybe someone have any examples? Thanks!
0
by: D-Someone | last post by:
We have an active directory user that just had her username renamed. When the user is running an application that calls our CurrentUser web service method (which returns the value of...
1
by: ranju | last post by:
I am trying to spawn a process (say an exe file) with different user crendentials than that of the current user. 1) Called LogonUserEx() to logon the user and recieve a handle to the token that...
2
by: abhighat4214 | last post by:
Hello all, I wanted to know if there is a PHP function which could display the name of the user that is currently logged in? Also could PHP code access and execute WIN32 API functions ? Kindly guide...
0
by: =?Utf-8?B?QW50b25pbyBPJydOZWFs?= | last post by:
We changed a username in AD. The user now logs on to a computer on our network using the new username without any problems. They can not logon using the old username. However, when they access a...
3
by: hkeiner | last post by:
I have an Access 2003 MDB database that uses the "currentuser()" function extensively (in queries and macros) to control what a particular user can see and do. In Access 2007 (which no longer uses...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.