判断某个程序是否已经启动

// 判断指定路径的Exe执行程序是否已经启动
// aExeName : 带全路径
function IsAppExeRun(const aExeName: string):Boolean;
var
  _vHandle : THandle;
  _vProEntry : TProcessEntry32;
  _vIsFound : Boolean;
  _vProHandle : THandle;
  _vHmodule : HMODULE;
  _vFileNameBuf : array[0..MAX_PATH] of Char;
  _vNeeded : DWORD;
  _vTempStr : string;
begin
  Result := False;
  _vHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  try
    _vProEntry.dwSize := SizeOf(TProcessEntry32);
    _vIsFound := Process32First(_vHandle, _vProEntry);
    while _vIsFound do
    begin
      _vTempStr := _vProEntry.szExeFile;
      if (UpperCase(_vTempStr) = UpperCase(ExtractFileName(aExeName))) or
        (UpperCase(_vTempStr) = UpperCase(aExeName)) then
      begin
        _vProHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
          False, _vProEntry.th32ProcessID);
        GetLastError;
        if _vProHandle <> 0 then
        begin
          if EnumProcessModules(_vProHandle, @_vHmodule, SizeOf(_vHmodule), _vNeeded) then
          begin
            ZeroMemory(@_vFileNameBuf, MAX_PATH + 1);
            GetModuleFileNameEx(_vProHandle, _vHmodule, _vFileNameBuf, MAX_PATH + 1);
            _vTempStr := _vFileNameBuf;
            if UpperCase(_vTempStr) = UpperCase(aExeName) then
            begin
              Result := True;
              Break;
            end;
          end;
        end;
      end;
      _vIsFound := Process32Next(_vHandle, _vProEntry);
    end;
  finally
    CloseHandle(_vHandle);
  end;
end;

你可能感兴趣的:(判断某个程序是否已经启动)