Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Delphi How to Get Computer Name

Author: admin | Category: Tips and Tricks, Windows API

The title is explain what we want to do right now. Yes, this is a delphi example how we can get the windows computer name with delphi application. This simple application need a TButton and a TEdit component. When we click the button the computer name fill the edit box.
Delphi Get Computer Name
Here's the code:

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
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);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := GetComputerName;
end;
 
end.
 

Tags: ,