I think I'm a little late for this question, but I will write something anyway for those who have the same problem.
My problem is similar to the fact that I want my application to be a graphical application, but running processes should run in the background without being tied to the interactive console window.
I managed to solve this using GenerateConsoleCtrlEvent (). The difficult part is that the documentation is not entirely clear how it can be used, and the pitfalls with it.
My decision is based on what is described here . But that also didn't explain all the details, so here are the details on how to make it work.
Create a new helper application, Helper.exe. This application will be between your application (parent) and the child process that you want to close. He will also create the actual child process. You must have this process "average person" or generated by GenerateConsoleCtrlEvent ().
Use some kind of IPC mechanism to communicate from the parent to the helper process, which the helper must close the child process. When the helper receives this event, it calls "GenerateConsoleCtrlEvent (CTRL_BREAK, 0)", which closes itself and the child process. I used the event object for this myself, which the parent terminates when he wants to cancel the child process.
To create your Helper.exe, create it using CREATE_NO_WINDOW and CREATE_NEW_PROCESS_GROUP. And when creating a child process, create it without the flags (0), i.e. it will output the console from its parent. Otherwise, it will ignore the event.
It is very important that each step is performed as follows. I tried all kinds of combinations, but this combination is the only one that works. You cannot send event CTRL_C. This will return success, but the process will be ignored. CTRL_BREAK is the only one that works. It doesn't matter much, because in the end they will call ExitProcess ().
You also cannot call GenerateConsoleCtrlEvent () with the process groupd identifier of the child process identifier, which directly allows the auxiliary process to continue to live. It will also fail.
I spent the whole day trying to get this to work. This solution works for me, but if anyone has something else, please add. I went to another network, finding many people with similar problems, but not a decisive solution to the problem. How GenerateConsoleCtrlEvent () works, it's also a bit weird, so if anyone knows more details about this, share it.
Shakta Mar 12 '10 at 8:13 2010-03-12 08:13
source share