WindowsPistha
WindowsPistha

Reputation: 100

Win32: Get message notification of other application's close/exit

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.

Upvotes: 5

Views: 5055

Answers (4)

tsragravorogh
tsragravorogh

Reputation: 3153

I recently ran into this problem and found a solution so wanted to share with you all. It all correct the way we should obtain handle to the process. Instead of WaitForSingleOBject though, I would recommend to use RegisterWaitForSingle object function. With this function you are giving a callback function and whenever the process exits, your callback function will be called. This is better than calling WaitForSingleObject in a thread. Calling WaitForSingleObject in your code by itself will cause your code to wait until the process exits. Here is an example of how to call it:

RegisterWaitForSingleObject(&waitHandle, processHandle, ProcessTerminatedCallback, param, INFINITE, WT_EXECUTEONLYONCE);

Where: [out]waitHandle - new handle created for you. Please note that you cannot use this handle to call CloseHandle, but you can wait on it, if you want to.

[in] processHandle - handle to the process that you are supposed to obtain yourself

[in] ProcessTerminatedCallback - the callback function that will be called when the process exits

[in] param - LPVOID parameter that will be passed to the callback

[in] INFINITE - either wait infinitely or for a specified time, look up MSDN for more info

[in] WM_EXECUTEONLYONCE - will call the callback function only once. look up MSDN for more info

Upvotes: 1

Dereck
Dereck

Reputation:

> Is there any other better way that I missed?

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

Upvotes: -1

Stefan
Stefan

Reputation: 43575

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.

Upvotes: 2

Serge Wautier
Serge Wautier

Reputation: 21878

  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.

Upvotes: 11

Related Questions