GetUserName and GetUserNameEx do not return the User Name
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
GetUserName and GetUserNameEx do not return the User Name
GetUserName_() and GetUserNameEx_() do not really return the current logged-in User Name as MS suggest, unless the PC is only used by one person (i.e. there is only one log-in). If the PC has more than one User, you can see that GetUserName actually returns the 'registered owner' name. Is there another function that can do the job better?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: GetUserName and GetUserNameEx do not return the User Nam
Seems to work here;
I have three more accounts on this machine, and two of these on the same domain the last is local only.
Code: Select all
UName$=Space(#UNLEN)
l=#UNLEN
If GetUserName_(UName$, @l)
Debug UName$
EndIf
The best preparation for tomorrow is doing your best today.
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: GetUserName and GetUserNameEx do not return the User Nam
GetUserName_() has always worked fine for me with XP and Win 7 with multiple user accounts. Can you post steps required to reproduce your problem? Thanks.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: GetUserName and GetUserNameEx do not return the User Nam
This is the code I am using:
GetUserNameEx is essentially the same but prefixes the User name with the Computer name.
I have tested this code on WinXP32bit and Win7 64bit.
Edit:
Code: Select all
Procedure.s GetUsrName()
;----------------------
Protected sUsrName.s
Protected size.i = (#UNLEN + 1) ;Max chars for Windows User Name (256) + null char
sUsrName = Space(size)
If Not GetUserName_(@sUsrName,@size)
sUsrName = "Name not verified"
EndIf
ProcedureReturn sUsrName
EndProcedure
I have tested this code on WinXP32bit and Win7 64bit.
Edit:
Retrieving Logged In User Name using a Windows ServiceThere might arise some situations when you need to have this information through a Windows service. If we use traditional API functions like GetUserName or GetUserNameEx, they simply return the user name of the process account under which that Windows service is running. It is the SYSTEM account in most cases. The above mentioned API functions always return SYSTEM as the currently logged in user.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: GetUserName and GetUserNameEx do not return the User Nam
Code: Select all
Debug GetEnvironmentVariable("USERNAME")
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: GetUserName and GetUserNameEx do not return the User Nam
Hi IdeasVacuum,
this post on German forum can probably help you..
http://www.purebasic.fr/german/viewtopi ... 17#p276417
with this code you can retrieve the right logged-on user equal in which context this running.
this post on German forum can probably help you..
http://www.purebasic.fr/german/viewtopi ... 17#p276417
with this code you can retrieve the right logged-on user equal in which context this running.
PB 5.10 Windows 7 x64 SP1
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: GetUserName and GetUserNameEx do not return the User Nam
Thanks jpd, that does look very interesting.
Although I won't be using the UserNametoStringSID() procedure, note that it crashes on FreeMemory(*sid).

Although I won't be using the UserNametoStringSID() procedure, note that it crashes on FreeMemory(*sid).
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: GetUserName and GetUserNameEx do not return the User Nam
aaaaaaaaah - same result as GetUserNameEx()
compiled as 32bit exe, run on Win7 64bit.
I tried GetEnvironmentVariable("USERNAME") before (and again now), no dice
A quick Google shows this up as a typical MS 'sloppy' issue.

I tried GetEnvironmentVariable("USERNAME") before (and again now), no dice

A quick Google shows this up as a typical MS 'sloppy' issue.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: GetUserName and GetUserNameEx do not return the User Nam
Hi,
I'm not really sure that I have right understand your purpose
you running this program on standalone machine ?
you running the program with system account?
with GetUserName:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then the returned username is "admin".
If running with system account the returned username is "system"
with GetUserNameEx:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then returned username is
"mycomp\admin".
If running with system accout the returned username is "system"
With WTSQuerySessionInformation:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then the returned username is
"admin", with system accout return admin
you can set the param to #true
the the name returned is
mycomp\admin in both cases.
here the new example.
I'm not know what the return value if more as 1 user logged in and this is running with system right.
Sorry for my English, hope my comments are readable.
I'm not really sure that I have right understand your purpose
you running this program on standalone machine ?
you running the program with system account?
with GetUserName:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then the returned username is "admin".
If running with system account the returned username is "system"
with GetUserNameEx:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then returned username is
"mycomp\admin".
If running with system accout the returned username is "system"
With WTSQuerySessionInformation:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then the returned username is
"admin", with system accout return admin
you can set the param to #true
the the name returned is
mycomp\admin in both cases.
here the new example.
Code: Select all
; coded by jpd, july 2012
EnableExplicit
#WTS_CURRENT_SERVER_HANDLE = 0
#WTS_CURRENT_SESSION = -1
Enumeration ;WTS_INFO_CLASS
#WTSInitialProgram=0
#WTSApplicationName
#WTSWorkingDirectory
#WTSOEMId
#WTSSessionId
#WTSUserName
#WTSWinStationName
#WTSDomainName
#WTSConnectState
#WTSClientBuildNumber
#wtsclientname
#WTSClientDirectory
#WTSClientProductId
#WTSClientHardwareId
#WTSClientAddress
#WTSClientDisplay
#WTSClientProtocolType
EndEnumeration
Prototype WTSQuerySessionInformation(hServer.l,SessionId.l,WtsInfoClass.l,ppBuffer.l,pBytesReturned.l)
Global WTSQuerySessionInformation.WTSQuerySessionInformation
Prototype WTSFreeMemory(lpBuffer.l)
Global WTSFreeMemory.WTSFreeMemory
Procedure LoadWTSAPI()
Protected Result
Result = OpenLibrary(#PB_Any, "wtsapi32.dll")
If Result
WTSFreeMemory=GetFunction(Result, "WTSFreeMemory")
CompilerIf #PB_Compiler_Unicode
WTSQuerySessionInformation = GetFunction(Result, "WTSQuerySessionInformationW")
CompilerElse
WTSQuerySessionInformation = GetFunction(Result, "WTSQuerySessionInformationA")
CompilerEndIf
If WTSQuerySessionInformation = 0
CloseLibrary(Result)
Result = 0
EndIf
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s CurrentSessionUserName(domain.b=#False)
Protected pBytesReturnedaddress.l
Protected UnameLength
Protected Username.s
Protected DomainName.s
Protected libWTS
Protected *username, *domainname
Protected WTSQuery
libWTS=LoadWTSAPI()
If libWTS
*username=AllocateMemory(256)
WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSUserName ,@*Username, @pBytesReturnedaddress)
UnameLength=MemoryStringLength(*Username)
Username=PeekS(*Username,UnameLength)
WTSFreeMemory(*Username)
If domain
*domainname=AllocateMemory(256)
WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSDomainName,@*domainname, @pBytesReturnedaddress)
UnameLength=MemoryStringLength(*domainname)
DomainName=PeekS(*domainname,UnameLength)
WTSFreeMemory(*domainname)
EndIf
CloseLibrary(libWTS)
EndIf
If Len(DomainName) > 0 And domain = #True
ProcedureReturn DomainName+"\"+Username
Else
ProcedureReturn Username
EndIf
EndProcedure
Debug CurrentSessionUserName()
Sorry for my English, hope my comments are readable.
PB 5.10 Windows 7 x64 SP1
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: GetUserName and GetUserNameEx do not return the User Nam
Thanks for getting back to me jpd. In fact, it is nearly impossible to correctly return the logged-in and currently active User Name 100% of the time, especially if the PC manufacturer has 'customized' the OS. - MS admit this fact when you read between the lines of numerous kb articles. I have decided not to worry about it, since the correct Name will be returned most of the time. When the correct Name is not returned, the User can manually enter it.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.