;下面是去年计算机世界上的一段代码,功能是查找出所有的进程,如果点击按钮则会终止进
程,我懒的修改代码符合你的需求,但是肯定可以满足你的要求:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,TLHelp32,
StdCtrls;
type
TProcessInfo=Record
ExeFile:String;
ProcessId
WORD;
End;
ProcessInfo=^TProcessInfo;
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ProcessList(var PList:TList);
procedure Button1Click(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
rocessInfo;
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.FormCreate(Sender: TObject);
var i:Integer;
p
RocessInfo;
begin
current:=TList.Create;
Current.Clear;
ListBox1.Clear;
ProcessList(Current);
for i:=0 to Current.Count-1 do Begin
new(p);
p:=Current.Items
;
ListBox1.Items.Add(p.ExeFile);
End;
end;
procedure TForm1.Button1Click(Sender: TObject);
var h:THandle;
aWORD;
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
ListBox1.Clear;
FormCreate(Self);
End;
End;
end;
end.