I’ve searched the web, and various forums, but I can’t find one thing, how to continually monitor open programs and then close another program (not the one being monitored) if something happens to the program being monitored.
For example, say that there is an already open Notepad window, and then the user or some other program opens a Windows Explorer window. I want to know how to close the Notepad window without interacting with the Windows Explorer window (other than realizing that it is indeed open), as well as closing the Notepad window if the user closes the Windows Explorer window.
Thanks in advance! 😀
On windows, you can use PSAPI (The Process Status API) to know when processes are started and terminate. The EnumProcesses function can give you this information.
A more reliable method to determine that a process terminated (since process ids can be reused) is to wait on its process handle (you will need the
SYNCHRONIZEaccess right) which you can obtain using OpenProcess and the process’ id obtained from EnumProcesses.To terminate a process, there is always TerminateProcess. To call TerminateProcess, you will need a handle to the process with the
PROCESS_TERMINATEaccess right. All of this assumes that you have the privileges needed to perform these actions on the process to be terminated.