[Solved] Get REAL logged-in user name

Windows specific forum
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

[Solved] Get REAL logged-in user name

Post 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?
Last edited by BarryG on Thu Mar 21, 2024 1:31 am, edited 1 time in total.
normeus
Enthusiast
Enthusiast
Posts: 472
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Get REAL logged-in user name

Post 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.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Get REAL logged-in user name

Post 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.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Get REAL logged-in user name

Post 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:
Last edited by Quin on Thu Mar 21, 2024 4:13 am, edited 1 time in total.
normeus
Enthusiast
Enthusiast
Posts: 472
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Get REAL logged-in user name

Post 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
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: Get REAL logged-in user name

Post by fryquez »

BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Get REAL logged-in user name

Post 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!
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

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

Post by RichAlgeni »

I just stumbled into this situation too!!! Very frustrating!!! This saved me a lot of time!
Post Reply