Delphi监视进程并结束进程
监视进程并结束进程在很多地方都用到这里借前人的经验写了个小例子:
以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监视进程并结束进程
,温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69854.html