Beranda > Delphi > Getting the username from the local PC

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;

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: