name.pas

来自「an example of delphi modifying the name 」· PAS 代码 · 共 66 行

PAS
66
字号
unit name;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
        ComputerName: PChar;
        size: DWord;
begin
        GetMem(ComputerName, 255);
        size := 255;
        // 获取计算机名称
        if GetComputerName(ComputerName,size)=False then
        begin
                MessageBox( Handle, '获取计算机名称失败。' , '错误' , MB_OK+MB_ICONERROR);
                FreeMem( ComputerName );
                Exit ;
        end;
        Edit1.Text := ComputerName;
        FreeMem(ComputerName);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
        ComputerName: String;  //保存计算机名的String
        result: bool;
        error: dword;
begin
        // 设置计算机名称
        ComputerName:=Edit1.Text;
        result:=SetComputerName(PChar(ComputerName));
         //判断是否正确执行了SetComputerName
        if result=false then
        begin
                error:=GetLastError();
                MessageBox( Handle, '设置计算机名称失败' , '错误' , MB_OK+MB_ICONERROR);
                Exit ;
        end;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?