Topic: Getting the real Windows Username

I just noticed that using the function GetUserName does not get the full username but the name of the folder containing the profile of the user. Is there a way to get the real full windows user name as displayed in the Windows Info (Windows 11)

Re: Getting the real Windows Username

Hello Tcoton

I used this code with RAD XE3 with success.

function TForm1.GetCurrentUserName: string;
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;

and to retrieve user's name :

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetCurrentUserName);
end;

I have not yet tested under MVD

JB

Re: Getting the real Windows Username

I tried something similar from a Delphi website but MVDB does not recognize DWORD (unknown type:'DWORD') and it does not recognize PChar either...