Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetLoggedUserName returns empty string on WSL (include possible fix) #127

Open
vesnx opened this issue Mar 18, 2024 · 2 comments
Open

GetLoggedUserName returns empty string on WSL (include possible fix) #127

vesnx opened this issue Mar 18, 2024 · 2 comments

Comments

@vesnx
Copy link

vesnx commented Mar 18, 2024

I tested the GetLoggedUserName and found that it is possebly not implemented the right way as for me getlogin reurns a $0000000000000000 (empty string)

I left the original code , removed the cast error for UTF8ToString and look at the "GetEnvironmentVariable" for USER or LOGINNAME

This seems to reflect my user name on all system WSL, VM or physical

Perhaps you consider it in your update

function GetLoggedUserName: string;
{$IFDEF MSWINDOWS}
const
  cnMaxUserNameLen = 254;
var
  sUserName: string;
  dwUserNameLen: DWORD;
begin
  dwUserNameLen := cnMaxUserNameLen - 1;
  SetLength(sUserName, cnMaxUserNameLen);
  GetUserName(PChar(sUserName), dwUserNameLen);
  SetLength(sUserName, dwUserNameLen);
  Result := sUserName;
end;
{$ELSE}
{$IF DEFINED(FPC) AND DEFINED(LINUX)}

begin
  Result := GetEnvironmentVariable('USERNAME');
end;
{$ELSE}

var
{$IFNDEF NEXTGEN}
  plogin: PAnsiChar;
{$ELSE}
  plogin: MarshaledAString;
{$ENDIF}
begin
{$IFDEF POSIX}
  try
    plogin := getlogin;
{$IFDEF NEXTGEN}
    Result := string(plogin);
    // NEXTGEN platforms handle strings differently, so direct assignment might be fine.
{$ELSE}
    if Length(plogin) > 0 then
    begin
      // Assuming UTF-8 encoding for plogin. If the encoding is different, adjust accordingly.
      Result := UTF8ToString(plogin);
      // Correct way to convert UTF-8 encoded PAnsiChar to UnicodeString
    end
    else
    begin
      if GetEnvironmentVariable('USER') <> '' then
      begin
        Result := GetEnvironmentVariable('USER');
      end
      else if GetEnvironmentVariable('LOGNAME') <> '' then
      begin
        Result := GetEnvironmentVariable('LOGNAME');
      end;

    end;

{$ENDIF}
  except
    Result := 'N/A';
  end;
{$ELSE}
  Result := 'N/A';
{$ENDIF}
  // raise ENotImplemented.Create('Not Android GetLoggedUserName implemented!');
end;
{$ENDIF}
{$ENDIF}
{$IFDEF IOS}
@vesnx
Copy link
Author

vesnx commented Mar 30, 2024

My sample would add an extra #0 to the end of the user name under windows, this fixes it

dwUserNameLen := cnMaxUserNameLen;
SetLength(sUserName, cnMaxUserNameLen);
if GetUserName(PChar(sUserName), dwUserNameLen) then
begin
  // Subtract 1 to exclude the null terminator from the length
  SetLength(sUserName, dwUserNameLen - 1);
end
else
  // Handle the error case where GetUserName fails
  SetLength(sUserName, 0);
Result := sUserName;

@exilon
Copy link
Owner

exilon commented Jul 3, 2024

Thanks, i will update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants