检测进程是否存在,简单方法。

function CheckProcessExist(const AFileName: string): Boolean;
var
//用于获得进程列表
hSnapshot: THandle;
//用于查找进程
lppe: TProcessEntry32;
//用于判断进程遍历是否完成
Found: Boolean;
begin
Result := False;
//获得系统进程列表
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//在调用Process32FirstAPI之前,需要初始化lppe记录的大小
lppe.dwSize := SizeOf(TProcessEntry32);
//将进程列表的第一个进程信息读入ppe记录中
Found := Process32First(hSnapshot, lppe);
while Found do
begin
if ((UpperCase(ExtractFileName(lppe.szExeFile)) = UpperCase(AFileName)) or (UpperCase(lppe.szExeFile) = UpperCase(AFileName))) then
begin
Result := True;
end;
//将进程列表的下一个进程信息读入lppe记录中
Found := Process32Next(hSnapshot, lppe);
end;
end;

procedure TForm4.btn1Click(Sender: TObject);
begin
if CheckProcessExist(Trim(edt1.Text)+'.exe') then
begin
ShowMessage('检测到了');
end else begin
ShowMessage('不存在');
end;
end;

(转至:http://blog.qdac.cc/?p=4177)

posted @ 2017-03-14 23:18  penginfo  阅读(1526)  评论(0编辑  收藏  举报