Run arbitrary subprocesses on Windows and end completely?

I have an application A that I would like to be able to call arbitrary other processes as specified by the user in the configuration file.

The script B package is one such process that the user would like to call A. B configures some environment variables, displays some messages, and calls the C compiler to do some work.

Is Windows provided in a standard way to terminate arbitrary processes? Suppose A is running in the console and gets CTRL + C. Can this pass this to B and C? Suppose A is running in a window and the user is trying to close the window, can he cancel B and C?

TerminateProcess is an option, but not a very good one. If A uses TerminateProcess on B, C continues to work. This can cause unpleasant problems if C works for a long time, since we can run another instance of C to work with the same files, while the first instance of C is still working secretly. In addition, TerminateProcess does not produce a clean output.

GenerateConsoleCtrlEvent sounds good and can work when everything works in the console, but the documentation says that you can send CTRL + C only to your own console, and therefore would not help if A was executed in the window.

Is there a SIGINT equivalent on Windows? I would really like to find an article like this: http://www.cons.org/cracauer/sigint.html for Windows.

+13
windows copy-paste process long-running-processes
Sep 21 '09 at 9:01
source share
2 answers

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.

+8
Mar 12 '10 at 8:13
source share

As @Shakta said, GenerateConsoleCtrlEvent() very complex, but you can send Ctrl + C without an auxiliary process.

 void SendControlC(int pid) { AttachConsole(pid); // attach to process console SetConsoleCtrlHandler(NULL, TRUE); // disable Control+C handling for our app GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); // generate Control+C event } 
+8
Oct. 15
source share



All Articles