Win32: Get notification of another application message close / close
My application needs to monitor all other running applications on the system. Is there a way to get a notification to quit every exe app?
Methods I could find:
1) Use PSAPI functions to get a list of running exes at frequent intervals. In each survey, compare with the previous list to find which application / process exited. Disadvantage: constant polling is required, will take up processor time.
2) Set a global hook for the WM_CLOSE message: using this, I get a notification when any application is closed by the close button in the title bar.
Disadvantage: (-) Not all applications generate the WM_CLOSE message (example: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or the button (for example, "File-> Exit"), I cannot capture this message
Is there any other better way I missed? Please advise.
a source to share
- Get a list of PIDs using PSAPI.
- Then create a handle for each process using OpenProcess () .
- Use WaitForMultipleObjects () to signal the exit of one of the processes.
a source to share
You can try the RegisterShellHookWindow () API and filter the HSHELL_WINDOWCREATED and HSHELL_WINDOWDESTROYED messages.
Of course, this will only notify you about apps that have a window.
a source to share
I recently ran into this problem and found a solution, so I wanted to share with you all. All of this fixes how we should handle the process. However, instead of WaitForSingleOBject, I would recommend using the RegisterWaitForSingle object function. With this function, you provide a callback function and whenever the process exits, the callback function is called. This is better than calling WaitForSingleObject on the thread. Calling WaitForSingleObject in your code will by itself make your code wait until the process has finished. Here's an example of how to call it:
RegisterWaitForSingleObject(&waitHandle, processHandle, ProcessTerminatedCallback, param, INFINITE, WT_EXECUTEONLYONCE);
Where: [out] waitHandle is a new handle created for you. Note that you cannot use this handle to call CloseHandle, but you can wait for it if you like.
[in] processHandle - handle the process you should get yourself
[in] ProcessTerminatedCallback is a callback function that will be called when the process exits
[in] param - LPVOID parameter that will be passed to the back message
[in] INFINITE - either wait indefinitely or for a specific amount of time, search MSDN for more information
[in] WM_EXECUTEONLYONCE - will call the callback function only once. search MSDN for more information
a source to share
> Is there any other better way I missed?
Yes many. See Win32 group (system notifications without any errors)