How to Find Out if a Program is Running

I received tip from Mike Gajewski, one of our readers. This is the code you need to find out if a certain program is running. The IsProcess() routine checks if the specific filename is running. This tip can be useful when your application should or needs some other application running in order to run properly/at all.

OK, here are the sources of unit EnumProc:

uses
  SysUtils, Classes;
...
const
  MAX_PATH = 260;
  TH32CS_SNAPPROCESS = 2;
...
type
  TProcessEntry32 = packed record
    iSize,
    iUsage,
    iProcessID,
    iDefaultHeapId,
    iModuleId,
    iThreads,
    iParentProcessId,
    iPriClassBase,
    iFlags: Integer;
    aExeFile: array[0..MAX_PATH] of Char;
  end;

function CreateToolHelpSnapShot(lFlags, lProcessId: Longint): Longint; stdcall;
function ProcessFirst(hSnapshot: longint; var uProcess: TProcessEntry32): Longint; stdcall;
function ProcessNext(hSnapshot: Longint; var uProcess: TProcessEntry32): Longint; stdcall;
procedure CloseHandle(hPass: Longint); StdCall;

function IsProcess(sExe: string): Boolean;
procedure GetProcesses(var ProcessList: TStringList);

implementation

function CreateToolHelpSnapShot; external 'kernel32.dll' name 'CreateToolhelp32Snapshot';
function ProcessFirst; external 'kernel32.dll' name 'Process32First';
function ProcessNext; external 'kernel32.dll' name 'Process32Next';
procedure CloseHandle; external 'kernel32.dll';

function IsProcess(sExe: string): Boolean;
(*
** This routine examines Windows processes currently running to see if a
** certain program is running.
**
** sExe  : The executable name of the program to check.
** Result: True if the program is running, False otherwise.
*)
var
  liI, lSnapShot: Longint;
  rProcess: TProcessEntry32;
begin
  Result := False;
  ExeName := UpperCase(ExeName);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.lSize := SizeOf(rProcess);
    lI := ProcessFirst(lSnapShot, rProcess);
    while lI <> 0 do begin
      if Pos(ExeName, UpperCase(rProcess.sExe)) <> 0 then begin
        Result := True;
        Break;
      end;
      lI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

procedure GetProcesses(var ProcessList: TStringList);
(* This procedure will retrieve a list of Windows processes currently running.
**
** ProcessList: TStringList variable to fill with the running process names.
**
** !!Note: ProcessList will be empty if an error occurs
*)
var
  lI, lSnapShot: Longint;
  rProcess     : TProcessEntry32;
begin
  ProcessList.Clear;
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.lSize := SizeOf(rProcess);
    lI := ProcessFirst(lSnapShot, rProcess);
    while r <> 0 do begin
      ProcessList.Add(rProcess.sExeFile);
      lI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

Example:

IsProcess('notepad.exe'); // Will return True if Notepad is running

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login