uses tlhelp32; 假设要终止的程序的文件名为:project2.exe,那么例程如下: var lppe:tprocessentry32; sshandle:thandle; hh:hwnd; found:boolean; begin sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0); found:=process32first(sshandle,lppe); while found do begin //进行你的处理其中lppe.szExefile就是程序名。 if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' then begin hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID); TerminateProcess(hh,0); end; found:=process32next(sshandle,lppe); end; end;
贴一段unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs, StdCtrls;type TProcessInfo = record ExeFile: string; ProcessId: DWORD; end; ProcessInfo = ^TProcessInfo; TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure ProcessList(var pList: TList); procedure My_RunFileScan(ListboxRunFile: TListBox); procedure Button2Click(Sender: TObject); private { Private declarations } public Current: TList; { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ProcessList(var pList: TList); var p: ProcessInfo; ok: Bool; ProcessListHandle: THandle; ProcessStruct: TProcessEntry32; begin PList := TList.Create; PList.Clear; ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0); ProcessStruct.dwSize := Sizeof(ProcessStruct); ok := Process32First(ProcessListHandle, ProcessStruct); while Integer(ok) <> 0 do begin new(p); p.ExeFile := ProcessStruct.szExeFile; p.ProcessID := ProcessStruct.th32ProcessID; PList.Add(p); ok := Process32Next(ProcessListHandle, ProcessStruct); end; end;procedure TForm1.Button1Click(Sender: TObject); var h: THandle; a: DWORD; p: PRocessInfo; begin if ListBox1.ItemIndex >= 0 then begin p := Current.Items[ListBox1.ItemIndex]; h := openProcess(Process_All_Access, true, p.ProcessID); GetExitCodeProcess(h, a); if Integer(TerminateProcess(h, a)) <> 0 then begin My_RunFileScan(ListBox1); end; end else Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK); end;procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox); var i: Integer; p: PRocessInfo; begin current := TList.Create; Current.Clear; ListboxRunFile.Clear; ProcessList(Current); for i := 0 to Current.Count - 1 do begin new(p); p := Current.Items[i]; ListboxRunFile.Items.Add(p.ExeFile); end; end;procedure TForm1.Button2Click(Sender: TObject); begin My_RunFileScan(ListBox1); end;end.
比如终止一个记事本的进程。 procedure TForm1.Button1Click(Sender: TObject); var H:THandle; P:DWORD; begin H:=FindWindow(nil,'无标题 - 记事本'); if H<>0 then begin GetWindowThreadProcessId(H,@P); if P<>0 then TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF); end; end;
假设要终止的程序的文件名为:project2.exe,那么例程如下:
var
lppe:tprocessentry32;
sshandle:thandle;
hh:hwnd;
found:boolean;
begin
sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);
found:=process32first(sshandle,lppe);
while found do
begin
//进行你的处理其中lppe.szExefile就是程序名。
if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' then
begin
hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);
TerminateProcess(hh,0);
end;
found:=process32next(sshandle,lppe);
end;
end;
Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs,
StdCtrls;type
TProcessInfo = record
ExeFile: string;
ProcessId: DWORD;
end;
ProcessInfo = ^TProcessInfo;
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ProcessList(var pList: TList);
procedure My_RunFileScan(ListboxRunFile: TListBox);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
Current: TList;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ProcessList(var pList: TList);
var
p: ProcessInfo;
ok: Bool;
ProcessListHandle: THandle;
ProcessStruct: TProcessEntry32;
begin
PList := TList.Create;
PList.Clear;
ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessStruct.dwSize := Sizeof(ProcessStruct);
ok := Process32First(ProcessListHandle, ProcessStruct);
while Integer(ok) <> 0 do
begin
new(p);
p.ExeFile := ProcessStruct.szExeFile;
p.ProcessID := ProcessStruct.th32ProcessID;
PList.Add(p);
ok := Process32Next(ProcessListHandle, ProcessStruct);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
h: THandle;
a: DWORD;
p: PRocessInfo;
begin
if ListBox1.ItemIndex >= 0 then
begin
p := Current.Items[ListBox1.ItemIndex];
h := openProcess(Process_All_Access, true, p.ProcessID);
GetExitCodeProcess(h, a); if Integer(TerminateProcess(h, a)) <> 0 then
begin
My_RunFileScan(ListBox1);
end;
end
else
Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK);
end;procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox);
var
i: Integer;
p: PRocessInfo;
begin
current := TList.Create;
Current.Clear;
ListboxRunFile.Clear;
ProcessList(Current);
for i := 0 to Current.Count - 1 do
begin
new(p);
p := Current.Items[i];
ListboxRunFile.Items.Add(p.ExeFile);
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
My_RunFileScan(ListBox1);
end;end.
procedure TForm1.Button1Click(Sender: TObject);
var
H:THandle;
P:DWORD;
begin
H:=FindWindow(nil,'无标题 - 记事本');
if H<>0 then
begin
GetWindowThreadProcessId(H,@P);
if P<>0 then
TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);
end;
end;