8

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;
Argalatyr
  • 4,639
  • 3
  • 36
  • 62
Alister
  • 6,527
  • 4
  • 46
  • 70
  • I usually make it even shorter by not using a separate buffer: SetLength(Result,256); SetLength(Result,GetComputerName(PChar(Result),255)); – Stijn Sanders Jul 21 '09 at 10:37
  • 1
    Stijn, you most certainly don't use that code. `GetComputerName` returns a `Bool` and doesn't accept a numeric literal for its second parameter. – Rob Kennedy Jul 21 '09 at 15:03
  • 4
    Alister, I advise you to use the named constant `Max_ComputerName_Length` for your buffer size instead of the magic numbers. – Rob Kennedy Jul 21 '09 at 15:05

7 Answers7

22

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

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
17

Another approach, which works well is to get the computer name via the environment variable. The advantage of this approach (or disadvantage depending on your software) is that you can trick the program into running as a different machine easily.

Result := GetEnvironmentVariable('COMPUTERNAME');

The computer name environment variable is set by the system. To "override" the behavior, you can create a batch file that calls your program, setting the environment variable prior to the call (each command interpreter gets its own "copy" of the environment, and changes are local to that session or any children launched from that session).

skamradt
  • 15,366
  • 2
  • 36
  • 53
13

GetComputerName from the Windows API is the way to go. Here's a wrapper for it.

function GetLocalComputerName : string;
    var c1    : dword;
    arrCh : array [0..MAX_PATH] of char;
begin
  c1 := MAX_PATH;
  GetComputerName(arrCh, c1);
  if c1 > 0 then
    result := arrCh
  else
    result := '';
end;
Conor Boyd
  • 1,024
  • 7
  • 15
4

What about this :

function GetComputerName: string;
var
  buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;
  Size: Cardinal;
begin
  Size := MAX_COMPUTERNAME_LENGTH + 1;
  Windows.GetComputerName(@buffer, Size);
  Result := StrPas(buffer);<br/>
end;

From http://exampledelphi.com/delphi.php/tips-and-tricks/delphi-how-to-get-computer-name/

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
villerose
  • 41
  • 2
  • I'm not sure if you're trying to answer this old question or if you're trying to write a new question?. Read the [faq](http://stackoverflow.com/faq) as this site works in a different way than traditional forums. – jachguate Apr 05 '11 at 15:21
4

If you want more than just the host name, you need GetComputerNameEx. Since there are many wrong implementations around (MAX_COMPUTERNAME_LENGTH is not enough, and 1024 is bad), here is mine:

uses Winapi.Windows;

function GetLocalComputerName(
  NameType: TComputerNameFormat = ComputerNameDnsHostname): string;
var
  len: DWORD;
begin
  len:= 0;
  GetComputerNameEx(NameType, nil, len); //get length
  SetLength(Result, len - 1);
  if not GetComputerNameEx(NameType, PChar(Result), len) then RaiseLastOSError;
end;

Valid values for the NameType parameter are:

  • ComputerNameDnsHostname, ComputerNameDnsDomain, ComputerNameDnsFullyQualified
  • ComputerNamePhysicalDnsHostname, ComputerNamePhysicalDnsDomain, ComputerNamePhysicalDnsFullyQualified
  • ComputerNameNetBIOS, ComputerNamePhysicalNetBIOS
maf-soft
  • 2,335
  • 3
  • 26
  • 49
2

I use this,

function GetLocalPCName: String;
var
    Buffer: array [0..63] of AnsiChar;
    i: Integer;
    GInitData: TWSADATA;
begin
    Result := '';
    WSAStartup($101, GInitData);
    GetHostName(Buffer, SizeOf(Buffer));
    Result:=Buffer;
    WSACleanup;
end;

Bye

RRUZ
  • 134,889
  • 20
  • 356
  • 483
0

This code works great, except when computer is on simple Workgroup and try to using GetLocalComputerName(ComputerNameDnsFullyQualified) returns computer name with a #0 (null) char at end, resulting in a bad processing of other charanters sent to a Memo component as a log. Just fix this issue checking for null at end.

function GetLocalComputerName(
  NameType: TComputerNameFormat = ComputerNameDnsHostname): WideString;
var
    len: DWORD;

begin

    len:= 0;
    GetComputerNameEx(NameType, nil, len);  //get length
    SetLength(Result, len - 1);
    if not GetComputerNameEx(NameType, PWideChar(Result), len) 
  then RaiseLastOSError;
    // fix null at end
    len := Length(Result);
    if (len > 2) and (Result[len] = #0) then
      Result := Copy(Result, 1, len-1);
end;
Danarman
  • 11
  • 2