How to prevent Ctrl + Break / Ctrl + C from closing both programs if one of them was launched using the system () or CreateProcess ()?

This is a test example:

(1). simple program that runs an infinite loop:

#include <iostream>
using namespace std;

int main() {
  int counter = 0;
  while (1) cout << ++counter << ": endless loop..." <<endl;
}

(2). another program running the above example with the command system():

#include <iostream>
#include <windows.h>

using namespace std;

int main() {
  system("endless_loop.exe");
  cout << "back to main program" << endl;
}

When executed Ctrl+Break, the program back to main programdoes not appear in this text . How to limit this key combination inside the process and return the progress bar back to the main application?

Another thing is that I do not always control the source code inside the program, so I can not change anything.

+5
source share
1 answer

Add this ::

#include <signal.h>

 ...

signal (SIGINT, SIG_IGN);

signal() Ctrl-Break. Linux fork()/exec().

Windows reset exec() - O/S+. , , Break, .

+3

All Articles