473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function to return Username (NT Login) of current user

14,534 Recognized Expert Moderator MVP
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)
  7. Function sys_OrigUserID() As String
  8. Dim s$, cnt&, dl&
  9. Dim max_String As Integer
  10. Dim username As String
  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
  20. End Function
May 29 '07 #1
4 36219
3,080 Recognized Expert Specialist
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...

May 29 '07 #2
32,564 Recognized Expert Moderator MVP
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
32,564 Recognized Expert Moderator MVP
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
  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
  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
32,564 Recognized Expert Moderator MVP
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

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
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 content based on the user's role. I wrote this to do it: if (HttpContext.Current.User.Identity.IsAuthenticated) { plLoggedIn.Visible = true;
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" password="MyPass" /> But if I ask for HttpContext.Current.User.Identity.Name it returns my personal Login name, in the format DOMAIN\USERNAME. Shouldn't...
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 applciation. I am using the 2.0 framework. I use the following syntax to retrieve the user name: string username = Environment.UserName; I use the...
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!
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 HttpContext.Current.User.Identity.Name), her old username is returned. DOMAIN\oldusername instead of DOMAIN\newusername. The user is logged in with this new...
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 represents the logged-on user. 2) Called ImpersonateLoggedOnUSer() to let the calling thread impersonate the security context of logged on user. ...
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 me regarding the same. Regards, Abhijit
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 IIS 6.0 web site where a programmer compares system.web.httpcontext.current.user.identity.name to a lookup table containing usernames they could not...
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 user level security) the current user function now always returns "admin" , which pretty much breaks my database functionality. This database is used in...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.