delphi获取exe文件是否在运行,查看某个程序在进程中是否已经存在

delphi中如何查看某个程序在进程中是否已经存在 

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

 

例子

  if FindProcess( 'mysqld-nt.exe ') then

     memo1.Lines.Add( '发现SQL服务! ');

猜你喜欢

转载自www.cnblogs.com/yclizq/p/12185225.html