Stopping a process
I have a program which uses WinExec to start some external processes when I press a button. I was wondering what I would use to kill an external process by pressing a button in the same program (for example, when I press 'close' on my program, how could I make it kill processes X, Y and Z as well as itself?).
If you want an elegant way to manage another process, it is not as trivial as WinExec:
Process and Thread Functions (Windows)
Thanks - I have been looking around for days based on the stuff in there, but I can't find anything. Would it be possible to give me a little example for how to close a process when a button is pressed (i.e.
I just want to close other specific open programs when I run another one.
//Code to close desired processes
Note to mods: this is a win32 programming question, not a C++ question.
Assuming you have a handle to the process, it is as simple as TerminateProcess(handle, exitcode) where exitcode is an integer value to be returned. That is not usually a good idea if the program has windows (i.e. a GUI) though. In that case, it is often better to send appropriate messages to the main window of the application in order to tell it to close down cleanly.
If you use CreateProcess() to create the process in the first place, one of the bits of information CreateProcess() gives back to the caller is a handle to the created process. You can also use EnumProcesses() to search through available processes, and find one with required characteristics.
I've been looking at EnumProcesses, but I'm a little confused with what I have to do - I suppose it's something like this:
...but I don't know what to do after that! Is it quick to make an example?
EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded )
I have changed it so that I use ShellExecute to start the processes - could anyone provide me with an example of how to close them by pressing a Win32 button?
ShellExecute does not return a handle to the new process, so does not provide enough information to allow your code to directly shut down that process. You will therefore need to find your process using EnumProcesses().
Preceding posts have given pointers on how to proceed. You really need to read the documentation from there. This link provides an example of usage of EnumProcesses() for printing out information about processes. All you need to do is change the PrintProcessNameAndID() function to something like DetectTheProcessToTerminateAndThenTerminateIt(). To do that, you need to identify some characteristic the process has that distinguishes it from other processes. The decision of how you do that is yours.