码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi监视进程并结束进程

时间:2015-07-13 17:51:30      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

监视进程并结束进程在很多地方都用到这里借前人的经验写了个小例子:

以QQ的进程qq.exe为例

关键代码如下:

 


function CheckTask(ExeFileName: string): Boolean;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := False;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop) <> 0 do begin
      if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =UpperCase(ExeFileName))
      or (UpperCase(FProcessEntry32.szExeFile) =UpperCase(ExeFileName))) then
        result := True;
      ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;

function KillTask(ExeFileName:string):integer;
const 
PROCESS_TERMINATE = $0001; 
var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle; 
FProcessEntry32: TProcessEntry32; 
begin 
Result := 0; 
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
FProcessEntry32.dwSize := SizeOf(FProcessEntry32); 
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); 
while Integer(ContinueLoop) <> 0 do
begin 
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = 
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = 
UpperCase(ExeFileName))) then 
Result := Integer(TerminateProcess( 
OpenProcess(PROCESS_TERMINATE, 
BOOL(0), 
FProcessEntry32.th32ProcessID), 
0)); 
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); 
end; 
CloseHandle(FSnapshotHandle); 
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if CheckTask(’qq.exe’)=true then
KillTask(’qq.exe’)
else
Label1.Caption:=’进程不存在,监视中...’;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
 if CheckTask(’qq.exe’)=true then
Label1.Caption:=’进程正在运行中...’
else
Label1.Caption:=’进程不存在,监视中...’;
end;

function CheckTask

function KillTask这两个函数在网上找的,这个例子一看就懂的,下面给出测试效果:

Delphi监视进程并结束进程

标签:

原文地址:http://www.cnblogs.com/FKdelphi/p/4643093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!