Beranda > Delphi > Get the username and computer name from a computer

Get the username and computer name from a computer

For Win95, you can find it in the registry - but this is not portable to NT. 
A safer way and one which is portable to NT) would be to use the Win32 calls GetComputerName and 
GetUserName, both of which are defined in the Windows unit.
Each of these functions takes a buffer as its first parameter and the length of the buffer as its second.
The function definitions are shown below:

function GetComputerName(lpBuffer: PChar; 
  var nSize: DWORD): BOOL; stdcall;
function GetUserName(lpBuffer: PChar; 
  var nSize: DWORD): BOOL; stdcall;


Use them like this:


 function  GetWindowsUserName : string;
const
  cnMaxLen=254;
var
  sUserName     : string;
  dwUserNameLen : DWord;
begin
  dwUserNameLen:=cnMaxLen-1;
  SetLength(sUserName, cnMaxLen);
  GetUserName(Pchar(sUserName), dwUserNameLen);
  SetLength(sUserName, dwUserNameLen);
  result:=sUserName;
  if dwUserNameLen=cnMaxLen-1 then
    result:='';
end;

Iklan
Kategori:Delphi
  1. September 20, 2016 pukul 7:34 am

    If you are interested in topic: earn online singapore visa – you should read about Bucksflooder first

  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: