BLUF: How to determine the current user logged in?
In a cross-platform environment, how do you determine the ID of the current system user? I dug through all of the documentation and could not find anything. Ideally this would be a simple property ( I assume within the framework ) that returns a string with the current user ID.
#If TargetCarbon
Declare Function CSCopyUserName Lib "Carbon" (useShortName As Boolean) As CFStringRef
Registered_NAME=CSCopyUserName(False)
#EndIf
#If TargetCocoa
Declare Function NSFullUserName Lib "Cocoa" As CFStringRef
Registered_NAME=NSFullUserName
#EndIf
If TargetWin32
Dim custregitem As RegistryItem
custRegItem = New RegistryItem("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
Registered_NAME=custregitem.value("RegisteredOwner").StringValue
#endif
Who is the user when you run a console app on a server with no one logged in ? In this case you might say “nil” or “no one” or “”
Or who is the “user” in a multi user web app ? In this case its not something the framework can determine as that depends on the session and who is on the other end of it in the web browser.
hmmm, good point. I started to suggest the idea of a portion of the framework to deal with system specific information. However, I just realized that would defeat the purpose of the framework being universally available regardless of platform.
[quote=21045:@Dave S] #If TargetWin32
Dim custregitem As RegistryItem
custRegItem = New RegistryItem(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion”)
Registered_NAME=custregitem.value(“RegisteredOwner”).StringValue #endif[/quote]
I tried this in a project I’m working on, and as the registry key suggests, it returns the registered owner’s name. The Windows Functionality Suite has this declare to get the logged-in, rather than the registered user:
[code] #if TargetWin32
Soft Declare Sub GetUserNameA Lib “AdvApi32” ( name as Ptr, ByRef size as Integer )
Soft Declare Sub GetUserNameW Lib “AdvApi32” ( name as Ptr, ByRef size as Integer )
dim mb as new MemoryBlock( 256 )
dim size as Integer = mb.Size()
if System.IsFunctionAvailable( "GetUserNameW", "AdvApi32" ) then
GetUserNameW( mb, size )
return mb.WString( 0 )
else
GetUserNameA( mb, size )
return mb.CString( 0 )
end if
For example, replacing “NSFullUserName” with “NSUserName” returns the name of the current home directory instead of the User Name (yes, not very helpfull
Other example: NSHomeDirectory will return:
“/Users/<current_user>”
<current_user> is the value returned by “NSUserName”.
This is from my test app for testing Windows subsystem for Linux (WSL) in windows 10. Hope this helps someone.
Dim Registered_NAME as string #If TargetCarbon then
Declare Function CSCopyUserName Lib “Carbon” (useShortName As Boolean) As CFStringRef
Registered_NAME=CSCopyUserName(False) #EndIf #If TargetCocoa then
Declare Function NSFullUserName Lib “Cocoa” As CFStringRef
Registered_NAME=NSFullUserName #EndIf
#If TargetWindows then
Dim custregitem As RegistryItem
custRegItem = New RegistryItem(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion”)
Registered_NAME=custregitem.value(“RegisteredOwner”).StringValue #EndIf
#if TargetLinux then
Var sh As New Shell
sh.Execute(“echo $USER”)
Registered_NAME = sh.Result.ToText #EndIf
[quote=489411:@Richard Kinning]#If TargetWindows then
Dim custregitem As RegistryItem
custRegItem = New RegistryItem(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion”)
Registered_NAME=custregitem.value(“RegisteredOwner”).StringValue #EndIf[/quote]
Aehm… this would return “localadmin” on my machine, which definitely is not the currently logged-in user’s name.
HKLM is the wrong place to look for current user’s info.
For TargetWindows:
If you just want the current user’s login/account-name: echo %USERNAME%
If you’re interested in the “full name”, have a look at GetUserNameExW function. There is a Thread in the Forum with an example.