views:

502

answers:

3

Hello ,

My application needs to monitor all other running applications on the system. Is there some way I could get notified on exit of every application exe?

The methods I could find:

1) Use PSAPI functions to get the list of running exes at frequent intervals. At each poll compare with the previous list to find which application/process has exited. Disadvantage: Requires constant polling, will take CPU time.

2) Set a global hook for WM_CLOSE message: Using this I would be able to get a notification when any application gets closed through the close button on the title bar

Disadvantage: (-)Not all the applications are generating a WM_CLOSE message(Ex: Total Video Player Exe) (-)If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message

Is there any other better way that I missed? Please advise.

+7  A: 
  1. Get a list of PIDs using PSAPI.
  2. Then get a handle on each process using OpenProcess().
  3. Use WaitForMultipleObjects() to be signalled when one of the processes exits.
Serge - appTranslator
+2  A: 

You could try the RegisterShellHookWindow() API and filter for HSHELL_WINDOWCREATED and HSHELL_WINDOWDESTROYED messages.

Of course, that will only get you notified about applications that have a window.

Stefan
I would avoid registering global hooks except as an extreme last resort.
jeffamaphone
A: 

> Is there any other better way that I missed?

Yes, plenty. See on Win32 group (system notifications, without any hook)