DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

ComputerName

Returns the name of the host computer.

function ComputerName: string;
var
  Buf: array[0..Windows.MAX_COMPUTERNAME_LENGTH] of Char; // for computer name
  BufSize: Windows.DWORD;                                 // size of name buffer
begin
  BufSize := SizeOf(Buf);
  if Windows.GetComputerName(Buf, BufSize) then
    Result := Buf
  else
    Result := '';
end;

UserName

Returns the name of the current user.

function UserName: string;
const
  UNLEN = 256;  // max size of user name buffer (per MS SDK docs)
var
  Buf: array[0..UNLEN] of Char;  // buffer for user name
  BufSize: Windows.DWORD;        // size of name buffer
begin
  BufSize := SizeOf(Buf);
  if Windows.GetUserName(Buf, BufSize) then
    Result := Buf
  else
    Result := '';
end;

View the whole database.

Go to the DelphiDabbler website.