Page 1 of 1

[Solved] Get REAL logged-in user name

Posted: Wed Mar 20, 2024 3:11 am
by BarryG
Consider this code when running from a standard non-admin logged-in account called "User":

Code: Select all

Debug UserName() ; Shows "User"
But if I enable admin rights and run it from the "User" account, it shows "Admin" instead. My app needs to work with the user's profile that is actually logged in to Windows. So how can I make it truly show which user is logged in at the time, regardless of the exe's elevation?

Re: Get REAL logged-in user name

Posted: Wed Mar 20, 2024 5:24 am
by normeus
Would this work:

Code: Select all

Debug GetEnvironmentVariable("USERNAME")
It shows my username but that might be because I am an admin of my computer.

Norm.

Re: Get REAL logged-in user name

Posted: Wed Mar 20, 2024 6:07 am
by BarryG
That doesn't help unfortunately. It also shows the admin name if you run it as admin when logged in as a standard account.

Re: Get REAL logged-in user name

Posted: Wed Mar 20, 2024 6:34 am
by Quin
How's this? It works for me, but I'm also logged in as an admin user.

Code: Select all

Define Res$ = Space(#UNLEN - 1)
Define L = #UNLEN
GetUserName_(Res$, @L)
Debug Res$
[EDIT]: Fixed null terminator :oops:

Re: Get REAL logged-in user name

Posted: Wed Mar 20, 2024 7:03 am
by normeus
How about something like this :

Code: Select all

Define Folder_ID,SFolderLoc.s
 If SHGetSpecialFolderLocation_(0, $28, @Folder_ID) = 0 ;CSIDL_PROFILE =$28
    SFolderLoc.s = Space(#MAX_PATH*2)
    SHGetPathFromIDList_(Folder_ID, @SFolderLoc)
    CoTaskMemFree_(Folder_ID)
  EndIf
  Debug sFolderLoc
  Debug StringField(sFolderLoc, 3,"\")
Norm

Re: Get REAL logged-in user name

Posted: Wed Mar 20, 2024 8:38 am
by fryquez

Re: Get REAL logged-in user name

Posted: Wed Mar 20, 2024 9:12 am
by BarryG
Quin and Normeus: Thanks for replying, but those snippets don't work either.
BINGO! :D This one correctly shows the current logged-in standard user name despite my exe running as admin. Thanks, Fryquez!

Re: [Solved] Get REAL logged-in user name

Posted: Wed May 29, 2024 10:27 pm
by RichAlgeni
I just stumbled into this situation too!!! Very frustrating!!! This saved me a lot of time!