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
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
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(lFl ags, 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(TH3 2CS_SNAPPR OCESS, 0);
if lSnapShot <> 0 then begin
rProcess.iSize := SizeOf(rProcess);
liI := ProcessFirst(lSnapShot, rProcess);
while liI <> 0 do begin
if Pos(EXEName, UpperCase(rProcess.aExeFil e)) <> 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(TH3 2CS_SNAPPR OCESS, 0);
if lSnapShot <> 0 then begin
rProcess.iSize := SizeOf(rProcess);
liI := ProcessFirst(lSnapShot, rProcess);
while liI <> 0 do begin
if Pos(EXEName, UpperCase(rProcess.aExeFil e)) <> 0 then
begin
GetExitCodeProcess(OpenPro cess(PROCE SS_TERMINA TE, BOOL(0),rProcess.iProcessI D), ExitCode); // Optional
GetExitCodeThread(OpenProc ess(PROCES S_TERMINAT E, BOOL(0),rProcess.iThreads) , ExitCode);
CloseHandle(OpenProcess(PR OCESS_TERM INATE, BOOL(0),rProcess.iProcessI D));
CloseHandle(OpenProcess(PR OCESS_TERM INATE, BOOL(0),rProcess.iThreads) );
Result := Integer(TerminateProcess(O penProcess (PROCESS_T ERMINATE, BOOL(0),rProcess.iProcessI D), 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(TH3 2CS_SNAPPR OCESS, 0);
if lSnapShot <> 0 then begin
rProcess.iSize := SizeOf(rProcess);
liI := ProcessFirst(lSnapShot, rProcess);
while liI <> 0 do begin
if Pos(EXEName, UpperCase(rProcess.aExeFil e)) <> 0 then
begin
GetExitCodeProcess(OpenPro cess(PROCE SS_TERMINA TE, BOOL(0),rProcess.iProcessI D), ExitCode); // Optional
GetExitCodeThread(OpenProc ess(PROCES S_TERMINAT E, BOOL(0),rProcess.iThreads) , ExitCode);
CloseHandle(OpenProcess(PR OCESS_TERM INATE, BOOL(0),rProcess.iProcessI D));
CloseHandle(OpenProcess(PR OCESS_TERM INATE, BOOL(0),rProcess.iThreads) );
Result := Integer(TerminateProcess(O penProcess (PROCESS_T ERMINATE, BOOL(0),rProcess.iProcessI D), 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.
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(lFl
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(TH3
if lSnapShot <> 0 then begin
rProcess.iSize := SizeOf(rProcess);
liI := ProcessFirst(lSnapShot, rProcess);
while liI <> 0 do begin
if Pos(EXEName, UpperCase(rProcess.aExeFil
Result := True;
Break;
end;
liI := ProcessNext(lSnapShot, rProcess);
end;
CloseHandle(lSnapShot);
end;
end;
procedure TForm1.Button1Click(Sender
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(TH3
if lSnapShot <> 0 then begin
rProcess.iSize := SizeOf(rProcess);
liI := ProcessFirst(lSnapShot, rProcess);
while liI <> 0 do begin
if Pos(EXEName, UpperCase(rProcess.aExeFil
begin
GetExitCodeProcess(OpenPro
GetExitCodeThread(OpenProc
CloseHandle(OpenProcess(PR
CloseHandle(OpenProcess(PR
Result := Integer(TerminateProcess(O
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(TH3
if lSnapShot <> 0 then begin
rProcess.iSize := SizeOf(rProcess);
liI := ProcessFirst(lSnapShot, rProcess);
while liI <> 0 do begin
if Pos(EXEName, UpperCase(rProcess.aExeFil
begin
GetExitCodeProcess(OpenPro
GetExitCodeThread(OpenProc
CloseHandle(OpenProcess(PR
CloseHandle(OpenProcess(PR
Result := Integer(TerminateProcess(O
Break;
end;
liI := ProcessNext(lSnapShot, rProcess);
end;
CloseHandle(lSnapShot);
end;
end;
procedure TForm1.Button2Click(Sender
begin
if IsProcess('notepad.exe') then
KillTask('notepad.exe');
end;
end.
with regards,
padmaja.
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(OpenPro cess(PROCE SS_TERMINA TE, BOOL(0),rProcess.iProcessI D), ExitCode); // Optional
GetExitCodeThread(OpenProc ess(PROCES S_TERMINAT E, 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
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(OpenPro
GetExitCodeThread(OpenProc
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ;)
- 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 ;)
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.
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.
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.
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.
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.