delphicomputer-name

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


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;

Solution

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