unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TForm1 = class(TForm)
btnKillHandle: TButton;
btnKillByPID: TButton;
lblProcess: TLabel;
lblPID: TLabel;
procedure FormCreate(Sender: TObject);
procedure btnKillHandleClick(Sender: TObject);
procedure btnKillByPIDClick(Sender: TObject);
private
procedure KillProcess(aPID: Cardinal);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
lblProcess.Caption := '当前进程虚拟句柄: ' + IntToStr(GetCurrentProcess);
lblPID.Caption := '当前进程 ID: ' + IntToStr(GetCurrentProcessId);
end;
procedure TForm1.btnKillHandleClick(Sender: TObject);
begin
TerminateProcess(GetCurrentProcess, 0);
end;
procedure TForm1.btnKillByPIDClick(Sender: TObject);
begin
KillProcess(GetCurrentProcessId);
end;
procedure TForm1.KillProcess(aPID: Cardinal);
var
tmpHandle: THandle;
begin
tmpHandle := OpenProcess(PROCESS_TERMINATE, False, aPID);
if (tmpHandle <> 0) then
TerminateProcess(tmpHandle,0);
end;
end.