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.
Type whoami and press Enter. Your current user name will be displayed.
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 .
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.
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;
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With