记得先在Uses中加入Tlhelp32单元。

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;

配合Delphi的结束进程代码,可以方便地监视进程和结束进程。

以查找QQ进程为例:
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;