Is there a way to kill my app's child process and perform it's cleanup(calling deconstructors and atexit functions), similarly to exit(exit_code)
, but on another process?

- 450
- 5
- 10
-
possible duplicate of http://stackoverflow.com/questions/6501522/how-to-kill-a-child-process-by-the-parent-process – Mare Infinitus Jun 16 '12 at 08:42
-
@MareInfinitus that's unix, i need a windows implementation – lazy_banana Jun 16 '12 at 11:10
2 Answers
If you are on windows, you probably start your child processes by CreateProcess, which has a PROCESS_INFORMATION as the last parameter.
Option 1:
This process information contains a handle to the process started in the hProcess member.
You can store this handle and use it to kill your child processes.
Insert You probably want to send WM_CLOSE and / or WM_QUIT? to "cleanly" end the process:
Here is a KB Article on what to do KB how to cleanly kill win32 processes
** End Insert**
Option 2:
Here is an discussion on how to properly kill a process tree: Terminate a process tree on windows

- 1
- 1

- 8,024
- 8
- 64
- 113
-
`ExitProcess` kills the process which called it, i need to kill the child from the parent, and `TerminateProcess` doesn't perform cleanup! – lazy_banana Jun 16 '12 at 11:28
-
As you do not provide information on how this processes are spawned and what kind of process they are, it is somewhat difficult. Sending a WM_CLOSE may be okay, or a GenerateConsoleCtrlEvent() could do the job. – Mare Infinitus Jun 16 '12 at 11:56
-
I use `CreateProcess` and im interested in a universal way of doing this, unlike `WM_CLOSE/WM_QUIT` which assumes that the process has a window – lazy_banana Jun 16 '12 at 12:00
-
1you should really try out WM_CLOSE. this is the gentle way of closing an application as seen here http://msdn.microsoft.com/en-us/library/ms632617%28v=vs.85%29.aspx if that application does not support WM_CLOSE, and not WM_QUIT, TerminateProcess may be the last resort. – Mare Infinitus Jun 16 '12 at 12:23
-
I'll try that out, then i guess there is no universal way of doing this, thanks – lazy_banana Jun 16 '12 at 13:13
There's no simple Win32 API for that kind of thing. The OS doesn't care what language your program's source code was written in, the compiled program appears to it as just a sequence of CPU instructions plus data.
The cleanest way would be to establish some kind of a communication channel between the processes (e.g. via shared memory) and simply request process termination.
You can achieve the same by starting the child process as a debugged process and then using debug APIs to alter the child's behavior, but that's too intrusive and not very straightforward to implement.

- 61,140
- 12
- 83
- 180
-
1The simplest and cheapest "communication channel" for this is a named event. – arx Jun 16 '12 at 09:28
-