Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get currently logged in username?

How do you get the currently logged in username in a Firemonkey Delphi project? I'm targeting both Windows and Mac.

I have XE2, but I suppose any version solution is fine because I will upgrade if needed.

like image 907
Marcus Adams Avatar asked Jul 11 '13 16:07

Marcus Adams


People also ask

How do I find my current username?

Type whoami and press Enter. Your current user name will be displayed.

How do I get to users in cmd?

Using %USERPROFILE% in Command Prompt If you use the Windows Command Prompt, you can use the cd command with %USERPROFILE% to change the current directory to your profile folder. Open the Command Prompt. For example, press Windows key + R to open the run box, then type cmd and press Enter .

How do I get the current username in Linux?

To quickly reveal the name of the logged in user from the GNOME desktop used on Ubuntu and many other Linux distributions, click the system menu in the top-right corner of your screen. The bottom entry in the drop-down menu is the user name.


3 Answers

For Windows you can use the GetUserName WinAPi function, for OSX you can use the NSUserName and/or NSFullUserName methods.

Try this sample for OSX

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Macapi.CoreFoundation,
  Macapi.Foundation,
  System.SysUtils;

function NSUserName: Pointer; cdecl; external '/System/Library/Frameworks/Foundation.framework/Foundation' name _PU +'NSUserName';
function NSFullUserName: Pointer; cdecl; external '/System/Library/Frameworks/Foundation.framework/Foundation' name _PU + 'NSFullUserName';

begin
  try
    Writeln(Format('User Name      %s',[TNSString.Wrap(NSUserName).UTF8String]));
    Writeln(Format('Full User Name %s',[TNSString.Wrap(NSFullUserName).UTF8String]))
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

For Windows

{$APPTYPE CONSOLE}


uses
  Windows,
  SysUtils;

function WUserName: String;
var
  nSize: DWord;
begin
 nSize := 1024;
 SetLength(Result, nSize);
 if GetUserName(PChar(Result), nSize) then
   SetLength(Result, nSize-1)
 else
   RaiseLastOSError;
end;


begin
  try
   Writeln(WUserName);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  readln;
end.

Compiled into a single unit:

uses
  {$IFDEF MACOS}
  MacApi.CoreFoundation, MacApi.Foundation,
  {$ENDIF}
  {$IFDEF MSWINDOWS}
  Windows,
  {$ENDIF}
  System.SysUtils;

{$IFDEF MACOS}
function NSUserName: Pointer; cdecl; external '/System/Library/Frameworks/Foundation.framework/Foundation' name '_NSUserName';
{$ENDIF}

function GetUserName: String;
{$IFDEF MSWINDOWS}
var
  nSize: DWord;
{$ENDIF}
begin
  {$IFDEF MACOS}
  Result := TNSString.Wrap(NSUserName).UTF8String;
  {$ENDIF}
  {$IFDEF MSWINDOWS}
  nSize := 1024;
  SetLength(Result, nSize);
  if Windows.GetUserName(PChar(Result), nSize) then
  begin
    SetLength(Result, nSize - 1)
  end
  else
  begin
    RaiseLastOSError;
  end
  {$ENDIF}
end;
like image 149
RRUZ Avatar answered Oct 08 '22 11:10

RRUZ


Another simpler solution is to get the computer name via the environment variable using GetEnvironmentVariable function as follows:

Result := GetEnvironmentVariable('USERNAME');

P.S.This solution is for Windows and Linux but you need to check your Delphi source code if it is supported.

like image 31
mas_oz2k1 Avatar answered Oct 08 '22 10:10

mas_oz2k1


A simpler answer for windows users

function CurrentUserName:String;  
var  
  UserName: array[0..127] of Char;  
  Size:DWord;  
begin  
  Size:=SizeOf(UserName);  
  GetUserName(UserName,Size);  
  Result:=UserName;  
end;  

from edn

like image 39
none Avatar answered Oct 08 '22 10:10

none