Detecting a running application

Any idea how to tell if a certain application is running i.e explorer.exe?
aroaraAsked:
Who is Participating?
 
MadshiConnect With a Mentor Commented:
That depends on the application. You can check for the existence of the explorer very easily by e.g. doing this:

  if FindWindow('Shell_TrayWnd', '') <> 0 then
    ShowMessage('Explorer is running!');

That means you can use FindWindow if you know the caption or the class of a window that ONLY this specific process, you're looking for, uses.

But it gets a bit more complicated, when you need to know that for applications where you don't know anything about which windows they open.

If you have D4 or D5, you can download the free unit "enumStuff.pas" from my homepage:

http://beam.to/madshi

Use the function GetProcessList to get a list of all currently running processes. This functions works under both win9x and winNT.
0
 
ipp1123Commented:
Dear aroara,

There is a way to check if a certain application is active and this is
not that difficult to accomplish but you have to use Win32 API. There
exists a function called FindWindow you can use to do the trick. I'll
give you an example with Notepad.exe:

Here is an OnClick event of a button:

procedure TForm1.Button1Click(Sender: TObject);
var WinHandle : HWND;
begin
 WinHandle:=FindWindow(nil, 'Untitled - Notepad');
 if WinHandle <> 0 then ShowMessage('Notepad Active!') else ShowMessage('Notepad Not Active!');
end;

The first parameter of FindWindow is the class name that can be left nil. For the second you
need to pass as a parameter the title of the main window for your program. The function doesn't
search for child windows. I really do believe this short fragment of code would help you.

Best regards, Ivan Petrov
0
 
MadshiCommented:
Hi Ivan, welcome to the EE-family...   :-)

Regards, Madshi.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ipp1123Commented:
Thanks for the warm welcome, Madshi!

Take care, Ivan.
0
 
the_realCommented:
Just a comment to the FindWindow function...

eg If you want to test if notepad is running and you test it with 'Untitled - Notepad', what then if the user have loaded a file, then it's 'myfile.txt - Notepad', or if the user uses another language of Windows, eg I'm Danish, so for me it would be: 'Ikke-navngivet - Notesblok'. Also, to madshi, if you are using another shell than Explorer, then there'll also be a 'Shell_TrayWnd', so you cant be certain that explorer is running just by checking 'Shell_TrayWnd'

Just my comment...

The_Real
0
 
MadshiCommented:
Yes, but the question is about a specific executable, not about the shell...   :-)
But you're right. Finding a window is not the best way. That's why I suggested using my process enumeration unit.

Regards, Madshi.
0
 
duke_nCommented:
watching...
0
All Courses

From novice to tech pro — start learning today.