What is the best way to get the local computer name in Delphi

The code must be compatible with D2007 and D2009.


My answer: Thanks to everyone who answered, I went 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;
+4
source share
6 answers

Windows API

+17
source

Another approach that works well is to get the computer name through an environment variable. The advantage of this approach (or a flaw depending on your software) is that you can easily trick the program as another machine.

Result := GetEnvironmentVariable('COMPUTERNAME');

. "" , , , ( "" , ).

+10

GetComputerName Windows API - . .

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;
+6

:

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;

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

+3

,

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

+2

sComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerNamesComputerName

-1

Source: https://habr.com/ru/post/1713109/


All Articles