c++consolesignalsatexit

C++: Execute function when program terminates


Some programs pop "Save before exit?" message when terminating. And I wonder if I can implement this with C++ console application. So I tried some standard functions like signal and atexit.

But they only work when:

So yeah, how? Is it only possible with GUI application?


Solution

  • In comments, you said:

    I want exit events to happen when that 'X' button is pressed(On windows). That's part of GUI I guess. Than what kind of request is sent to program when the exit button of the console is pressed?

    You can use SetConsoleCtrlHandler() to register a user defined callback function that receives a CTRL_CLOSE_EVENT notification when the console window is closed:

    A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking Close on the console window's window menu, or by clicking the End Task button command from Task Manager).