Getting the username from the local PC
   


Use this function to get the username from the local machine only. Will return '<not logged on>' if user is not logged onto a network.

{$DEFINE Delphi3Below}
{$IFDEF VER130} //Delphi 5
  {$UNDEF Delphi3Below}
{$ELSE}
  {$IFDEF VER120} //Delphi 4
    {$UNDEF Delphi3Below}
  {$ENDIF}
{$ENDIF}


function GetLocalUserName: string;
{ func to retrieve the local username if logged on. }
const
{$IFDEF Delphi3Below}
  NetUserNameLength: integer = 50;
{$ELSE}
  NetUserNameLength: Cardinal = 50;
{$ENDIF}
var
  UserNameStr: string;
begin
  SetLength(UserNameStr, NetUserNameLength);
  GetUserName(pChar(UserNameStr), NetUserNameLength);
  if GetLastError = ERROR_NOT_LOGGED_ON then
    Result := '<not logged on>'
  else
    begin
      SetLength(UserNameStr, StrLen(pChar(UserNameStr)));
      Result := UserNameStr;
    end;
end;


Blue Orb Software

[email protected]