bdn.borland.com

Article #26754: How to detect the computer name.

QUESTION:

How can I get the name of the computer my application is running on?

ANSWER:

On a Form, add a TButton and a TEdit box. Declare your function and add the GetCompName code, shown below, to the implementation section of the Unit. Next, in the TButton's OnClick event add the code shown. Now run the project and click the button.

function GetCompName: string;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetCompName: string;
var
  dwLength: dword;
begin
  dwLength := 253;
  SetLength(result, dwLength+1);
  if not Windows.GetComputerName(pchar(result), dwLength) then
    raise exception.create('Computer name not detected');
  result := pchar(result);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := GetCompName;
end;

Last Modified: 05-FEB-02