unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI, TLHelp32;
type
TProcessInfo = record
ExeFile: string;
ProcessId: DWORD;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure TerminateAProcess(ProcessExeName: String);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.TerminateAProcess(ProcessExeName:String);
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
Ret : BOOL;
h : THandle;
p : TPRocessInfo;
a : Cardinal;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32); //创建系统快照
Ret:=Process32First(FSnapshotHandle,FProcessEntry32); //先初始化 FProcessEntry32 的大小
while Ret do
begin
p.ExeFile:=FProcessEntry32.szExeFile;
p.ProcessId := FProcessEntry32.th32ProcessID;
if LowerCase(p.ExeFile)=LowerCase(ProcessExeName) then //你要修改的exe文件名
break;
Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
h := openProcess(Process_All_Access, true, p.ProcessID);
GetExitCodeProcess(h, a); //循环枚举出系统开启的所有进程,找出进程
TerminateProcess(h, a);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TerminateAProcess('taskmgr.exe');
end;
end.