Link to home
Start Free TrialLog in
Avatar of pjelias
pjelias

asked on

How can I check if an application is running or will be launched ?

Can anybody please advise me of how to detect if a certain application is running ie. Internet Explorer, and then close it ?

Also, is it possible to detect if an application is about to be launched and then prevent it from being launched ? If so how ?

I have Delphi 6.

I am basically wanting to write an application which will restirct PC usage for my teenage daughter.


Any help would be greatly appreciated

Regards
PJE
Avatar of Johnjces
Johnjces
Flag of United States of America image

If you go to the below link, you can get a component with source that shows easily how to check and see if an application, even Internet Explorer, is running.

ftp://jcitssystems.com/pub/DelphiComps/CESRURunning.zip

There is no way to know or tell if an application is about to be launched but it can be closed pretty quickly once it is detected.

Hope this helps ya.
Hello Sir,

  You could use the following function to check if an exe is already running.

unit Unit1;

interface

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

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;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

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';
  procedure SwitchToThisWindow(h1: hWnd; x: bool); stdcall;
  external user32 Name 'SwitchToThisWindow';
         {x = false: Size unchanged, x = true: normal size}
{$R *.dfm}

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;
  EXEName : String;
begin
  Result := False;
  EXEName := UpperCase(sExe);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.iSize := SizeOf(rProcess);
    liI := ProcessFirst(lSnapShot, rProcess);
    while liI <> 0 do begin
      if Pos(EXEName, UpperCase(rProcess.aExeFile)) <> 0 then begin
        Result := True;
        Break;
      end;
      liI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsProcess('notepad.exe') then
   ShowMessage('Notepad is open');
end;

function TForm1.KillTask(sExe: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  liI, lSnapShot: Longint;
  rProcess      : TProcessEntry32;
  EXEName : String;
begin
  Result := 0;
  EXEName := UpperCase(sExe);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.iSize := SizeOf(rProcess);
    liI := ProcessFirst(lSnapShot, rProcess);
    while liI <> 0 do begin
      if Pos(EXEName, UpperCase(rProcess.aExeFile)) <> 0 then
      begin
        GetExitCodeProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID), ExitCode);  // Optional
        GetExitCodeThread(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iThreads), ExitCode);
        CloseHandle(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID));
        CloseHandle(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iThreads));
        Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID), 0));
        Break;
      end;
      liI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

function TForm1.KillTask(sExe: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  liI, lSnapShot: Longint;
  rProcess      : TProcessEntry32;
  EXEName : String;
begin
  Result := 0;
  EXEName := UpperCase(sExe);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.iSize := SizeOf(rProcess);
    liI := ProcessFirst(lSnapShot, rProcess);
    while liI <> 0 do begin
      if Pos(EXEName, UpperCase(rProcess.aExeFile)) <> 0 then
      begin
        GetExitCodeProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID), ExitCode);  // Optional
        GetExitCodeThread(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iThreads), ExitCode);
        CloseHandle(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID));
        CloseHandle(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iThreads));
        Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID), 0));
        Break;
      end;
      liI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 if IsProcess('notepad.exe') then
   KillTask('notepad.exe');
end;

end.
 
with regards,
padmaja.
Avatar of pjelias
pjelias

ASKER

Padmaja,

thanks for the response, I have tried your solution but get the following error on the following lines

"Types of actual and formal var parameters must be identical"

        GetExitCodeProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID), ExitCode);  // Optional
        GetExitCodeThread(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iThreads), ExitCode);

I have Created a Form and a Unit.

I have put ALL code except the button clicks into the Unit, and obviously the button clicks on the form.

I had major errors if I added it all into the Form.

Regards
PJE
seeing if an application is about to be launched can be done using system wide hooking on a few functions: createprocess/shellexec to name 2.
see this open question: https://www.experts-exchange.com/questions/21976022/traping-and-void-if-user-want-to-run-some-application.html
ASKER CERTIFIED SOLUTION
Avatar of saravananvg
saravananvg
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hm... your question had 2 parts:
- detect a running application
- detect when an application is about to be launched.

saravananvg answered the first one, I answered the second. if my answer was not god, you should have mentioned that ;)
Avatar of pjelias

ASKER

Ciuly,

The accepted answer will do what I need to do.
Working code was provided, and not just links.

I took a look at the links you provided, way too difficult for my current level of programming, with little to no supporting documentation.

The above code is simple and can be adapted for my needs.

Also, from the comments provided by Johnjces
       "There is no way to know or tell if an application is about to be launched but it can be closed pretty quickly once it is detected."

I thought the code provided by saravananvg is close enough to the solution I was after.

Also, when you accept a solution, there is box to eneter any comments. Just a radio group for ratings.

Avatar of pjelias

ASKER

Also, when you accept a solution, there is NO box to enter any comments. Just a radio group for ratings.
true, but you can post the comments later ;)

and since I did give a solution I thought it is pretty obvious that Johnjces was wrong. and just because it is out of your reach, it doesn't mean that it is no answer. AND, that link has a demo in it so it does have source and it also contains some explanations.

anyway, no need to argue on this since the other question is still open and not a PAQ.