Re: Find out if an .exe is running

[ DelphiLand Discussion Forum ]

Posted by webmaster Guido on February 06, 2002:

In Reply to: Find out if an .exe is running posted by Alex Briseno on February 01, 2002:

: How can I find out if an specific .exe file is running in the PC?? I think that may be an INI function, but I'm new to Delphi, so I don't know which one
-------

Finding out if an application is running, can be done with some so-called Windows "API" functions. I won't give any details on why and how this works, I'll only give some examples that you can experiment with.

If you know the caption (the title) of an application's window, you can find out if there is a window with that title by using the function 'FindWindow'. Below is some example code. If you enter the word Calculator in the edit-box Edit1 and you click Button1, it tells you if Windows' Calculator is running:

procedure TForm1.Button1Click(Sender: TObject);
var
  H: Thandle;
begin
  H := FindWindow(nil, PChar(Edit1.Text));
  if H <> 0 then
    ShowMessage(Edit1.Text + ' is running')
  else
    ShowMessage(Edit1.Text + ' not running');
end;

Finding out from an application's exe-file is more complicated. You have to look for all the "processes" that are running, and check them one by one to see if their exe-file is what you are looking for.

For the next example to work, you first have to add TLHELP32 to the "uses" directive that is near the top of the form's unit. If for example it reads:

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

then change it to:

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, TLHELP32;

The following code checks if an app is running whose exe-filename is in Edit1, such as calc.exe:

procedure TForm1.Button1Click(Sender: TObject);
var
  IsRunning, DoTest: Boolean;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  IsRunning := False;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
  DoTest := Process32First(FSnapshotHandle, FProcessEntry32);
  while DoTest do begin
    IsRunning := 
      UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(Edit1.Text);
    if IsRunning then 
      DoTest := False  // stop the loop, we found it
    else 
      DoTest := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
  if IsRunning then 
    ShowMessage(Edit1.Text + ' is running')
  else 
    ShowMessage(Edit1.Text + ' is not running');
end;

If you want to check the *full* path of the exe, such as c:\windows\calc.exe, then remove the ExtractFileName function. Just change the code
IsRunning := UpperCase(ExtractFileName(...
to the following:

IsRunning := UpperCase(FProcessEntry32.szExeFile) = UpperCase(Edit1.Text); 

Because the API functions are not functions of Delphi itself, they are not documented in the regular Delphi help-files, but in the help file win32.hlp. If that one is on your PC, you might have a look... but be warned: this is quite advanced stuff.


[ DelphiLand Discussion Forum ]