PDA

View Full Version : How to monitor a Win32 application?


LoRd_MuldeR
4th April 2007, 22:40
Hi!

I'd like to write a small tool that can monitor another Win32 application and detect the occurrence of a certain message (dialog box). I don't need to send anything back to the host application, just monitor it and perform a certain task once the message occurs.

To be more concrete: I want to monitor Avidemux until the encoding process is completed and the "Done!" message appears. Then I want shut down the computer, which is an easy task to do. Only problem is to detect the right moment for shutdown.

BTW: I want to use Delphi7 to write my tool.

Any suggestions are welcome :)

jeffy
4th April 2007, 23:35
Something like this?
http://www.delphidabbler.com/articles?article=13&part=2#progrunning
(Finding if your program is running)

JohnnyMalaria
5th April 2007, 00:08
Windows provides a number of API functions that can help you:

FindWindow and FindWindowEx

You'll need to look at the documentation on MSDN and translate the syntax to suit Delphi.

Firstly, use FindWindow to find Avidemux' top-level window:

HWND FindWindow( LPCTSTR lpClassName,
LPCTSTR lpWindowName
);

lpClassName can be NULL.
lpWindowName will be a zero-terminated string with the name (i.e., caption) of the window to find - whatever Avidemux' main caption is.

HWND FindWindowEx( HWND hwndParent,
HWND hwndChildAfter,
LPCTSTR lpszClass,
LPCTSTR lpszWindow
);

hwndParent will be the top-level Avidemux window handle. This function will search all its child windows for a match. Set lpszWindow to the caption of the message dialog you are waiting on. The documentation isn't clear if lpszClass can be NULL or not. If not, and you don't know it, you'll have to use FindWindow() instead - but it will search all windows (not just Avidemux').

This all assumes that the message dialog's caption is meaningful (so it isn't confused with another process' windows). If not, you'll have to enumerate all the windows within each window to find out what resource items (buttons, static text, etc) are there.

If you have access to Spy++ (or non-MS equivalent), it will help you tremendously in finding out what window class, caption etc to search for.

I'd set up a Windows timer to perform the search every 5 minutes or so (or however often you feel is sufficient).

Sorry I can't help with specific Delphi (it's been a l-o-n-g time since I succumbed to the dark side and moved from Pascal to C++).

Note: a different approach would be to use Windows' process monitoring functions to monitor Avidemux' CPU usage and, when it has dropped to 0 for, say, 30 seconds, you can assume the conversion has finished. Again, I'd use a Windows timer and determine the CPU usage every so often. If you know that the only CPU-intensive process will be Avidemux, you could simply monitor the overall system CPU usage instead. (Later versions of the Windows API make it easier to get process-specific information but, typically, the Delphi support for these latest API functions is somewhat behind that found in Microsoft's compilers).

Hope this helps....

LoRd_MuldeR
5th April 2007, 00:21
Thanks :)

The problem is, the caption of Avidemux' main-window differs every time. It depends on the filename you process.
Furthermore the dialog window I'm waiting for has a blank caption :-(
So I will have to check for the control, which holds the text message. It's some GTK+ component I guess...


//EDIT

It looks like that:

http://img214.imageshack.us/img214/3130/fileadmdoneqx1.gif

LoRd_MuldeR
5th April 2007, 04:36
I think I found a solution:
Instead of waiting for the "Done" dialog to appear, I just wait for the "Encoding..." dialog to close.

Result can be found here:
http://forum.doom9.org/showpost.php?p=979570&postcount=396

LoRd_MuldeR
8th April 2007, 23:48
Here is a new version including sources:
http://mulder.brhack.net/public/downloads/ADM_Tools.2007-04-08.zip


//EDIT

And another optimization:
http://mulder.brhack.net/public/downloads/ADM_Tools.2007-04-09.zip

julesh
11th April 2007, 08:14
To be more concrete: I want to monitor Avidemux until the encoding process is completed and the "Done!" message appears. Then I want shut down the computer, which is an easy task to do. Only problem is to detect the right moment for shutdown.


Why not simply change avidemux so that it can do this itself? That's the power of open source...

LoRd_MuldeR
12th April 2007, 17:40
Why not simply change avidemux so that it can do this itself? That's the power of open source...

Well, Mean said that those are OS specific features and because Avidemux is primarily developed for Linux, this is Low Priority work. Once it will be implemented into Avidemux, my tool will become redundant. But until then it saves some work :cool:

masken
12th April 2007, 22:23
Another option is AutoIt:
http://www.autoitscript.com/