What's the best method for getting the local computer name in Delphi

Alister picture Alister · Jul 21, 2009 · Viewed 30k times · Source

The code needs to be compatible with D2007 and D2009.


My Answer: Thanks to everyone who answered, I've gone with:

function ComputerName : String;
var
  buffer: array[0..255] of char;
  size: dword;
begin
  size := 256;
  if GetComputerName(buffer, size) then
    Result := buffer
  else
    Result := ''
end;

Answer

Mark Wilkins picture Mark Wilkins · Jul 21, 2009

The Windows API GetComputerName should work. It is defined in windows.pas.