I think I'm a little late for this question, but I will write something anyway for those who have the same problem. This is the same answer as I gave this question.
My problem was that I would like my application to be a graphical application, but running processes should run in the background without connecting an interactive console window. I think this solution should also work when the parent process is a console process. You may need to remove the CREATE_NO_WINDOW flag.
I was able to solve this problem using GenerateConsoleCtrlEvent () using a wrapper application. 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 in fact, this did not explain all the details with an error, so here is the detailed information 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", otherwise GenerateConsoleCtrlEvent () will fail.
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 on the net, finding many people with similar problems, but without a specific solution to the problem. How GenerateConsoleCtrlEvent () works, it's also a bit weird, so if anyone knows more details about this, share it.