|
Q My application, MainApp, spawns another process, WorkerApp, to do some additional work. When WorkerApp is running, it may spawn even more child processes that do even more work. While MainApp is waiting for WorkerApp and its children to complete, the user may want to stop this additional work from continuing. To do this, I have MainApp kill WorkerApp, but this is not sufficientWorkerApp's child processes must terminate as well.
![]() Irfan Shariff
A I'm quite surprised that I haven't been asked this question before. This seems like a common thing to want to do, but Win32® doesn't offer an easy way to accomplish it. The Win32 specification states that no relationship exists between a parent process and any of its child processes once a child process has been created. Many other operating systems support a parent/child relationship policy, so if you kill a process, the system automatically kills all of the process's descendant processes.
|
Typedef.struct.tagPROCESSENTRY32 { DWORD dwSize; DWORD cntUsage; DWORD th32ProcessID; DWORD th32DefaultHeapID; DWORD th32ModuleID; DWORD cntThreads; DWORD th32ParentProcessID; // Look Here! LONG pcPriClassBase; DWORD dwFlags; char szExeFile[MAX_PATH]; } PROCESSENTRY32; |
th32ParentProcessID is the ID of the process that created the process I'm looking at. Now I can walk the set of processes in the system and determine which are descendants of the main process that I want to kill. I can then call OpenProcess, TerminateProcess, and CloseHandle for each of these descendant processes. I think you'll agree that this is not such a horrible solution.
![]() ![]() ![]() ![]() ![]() |
BOOL GenerateConsoleCtrlEvent( DWORD dwCtrlEvent, // pass CTRL_BREAK_EVENT here DWORD dwProcessGroupId // pass 0 here ); |
By default, when a console process receives a Ctrl+Break notification, the process terminates itself. What could
be easier?
![]() ![]() ![]() ![]() ![]() ![]()
![]() ![]() ![]() Have a question about programming in Win32? |
|
Terms of Use. |