How to determine current user

  1. 7 years ago

    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.

  2. Dave S

    18 Jul 2013 San Diego, California USA
          #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
  3. Excellent, although I find it mildly surprising that this is not included in the Framework.

    Thank you for the prompt response,

    Ean

  4. Norman P

    18 Jul 2013 Xojo Inc, Xojo Pro Seeking work. npalardy@great-w...

    Its not universally available :P

    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.

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

  6. Peter T

    23 Jul 2013 Testers, Xojo Pro Oakland, CA USA

    @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

    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:

      #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
  7. Emile S

    11 Aug 2013 Europe (France, Strasbourg)

    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 "/" ;-:) !

    What about Linux ?

  8. 2 weeks ago

    Richard K

    May 20 Testers, Xojo Pro

    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

  9. Richard K

    May 20 Testers, Xojo Pro

    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

  10. Christian S

    May 20 Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    SystemInfornationMBS module has UserName property.

    Cross platform.

  11. Jürg O

    May 20 Testers, Xojo Pro
    Edited 2 weeks ago

    @Richard K #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

    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 .

  12. Lennox J

    May 20 Jamaica. West Indies

    How about SpecialFolder.Documents.Parent.Name

  13. Christian S

    May 20 Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  14. Lennox J

    May 20 Jamaica. West Indies

    Thanks Christian,
    Didn't know that
    Lennox

or Sign Up to reply!