How to determine current user

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.

Thank you in advance for any help.

      #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

Excellent, although I find it mildly surprising that this is not included in the Framework.

Thank you for the prompt response,

Ean

Its not universally available :stuck_out_tongue:

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

#endif[/code]

Hi,

Cocoa: more data can be found in the file NSPathUtilities.h
http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html
Under: Managing File Paths

http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Foundation_Functions.pdf

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”.

NSOpenStepRootDirectory will return “/” ;-:slight_smile: !

What about Linux ?

For linux use this to get the current user.

#if TargetLinux then
Dim sh As New Shell
sh.Execute(“echo $USER”)
Registered_NAME = sh.Result.ToText
#EndIf

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

Print "Hello " + Registered_NAME
print "Locale " + xojo.core.Locale.Current.Identifier
print datetime.now.ToString

print “”

print" Press a key…"
dim s as string = input

SystemInfornationMBS module has UserName property.

Cross platform.

[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.

How about SpecialFolder.Documents.Parent.Name

Bad idea as name and folder name don’t need to be in sync.

Thanks Christian,
Didn’t know that
Lennox