探测当前的系统中进程有那些,如进程中包含某个游戏(即找到到游戏,要摸关闭,要摸警告,弹出一窗口)
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,tlhelp32;
type
tprocinfo=record
files:string;
procid:dword;end;
procinfo=^tprocinfo;
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
cur:tlist;
procedure proclist(var penum:tlist);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.proclist(var penum: tlist);
var//找到目前所有的执行程序,同时得到其路径,进程ID
proc:procinfo;
getproc:bool;
proclisthandle:thandle;
procstruct:tprocessentry32;
begin
proclisthandle:=createtoolhelp32snapshot(
th32cs_snapprocess,0);
procstruct.dwSize:=sizeof(procstruct);
getproc:=process32first(proclisthandle,procstruct);
while integer(getproc)<>0 do
begin
new(proc);
proc.files:=extractfilename(procstruct.szExeFile);
proc.procid:=procstruct.th32ProcessID;
penum.Add(proc);
getproc:=process32next(proclisthandle,procstruct);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
pinfo:procinfo;
begin
cur:=tlist.Create;
cur.Clear;
listbox1.Clear;
proclist(cur);
for i:=0 to cur.Count-1 do
begin
new(pinfo);
pinfo:=cur.Items[i];
listbox1.Items.add(pinfo.files);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);//关闭所选的进程,
var h:thandle;
a:dword;
p:procinfo;
begin
if listbox1.ItemIndex>=0 then
begin
p:=cur.Items[listbox1.itemindex];
h:=openprocess(process_all_access,true,p.procid);
getexitcodeprocess(h,a);
if integer(terminateprocess(h,a))<>0 then
begin
listbox1.Clear;
formcreate(self);
end;
end;
end;
end.
当然,你可以在BUTTON1CLICK中随意改为你想要的代码。比如弹出一警告窗口,