C++: Execute function when program terminates - c++

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:
main() returns (atexit)
sending interrupt through Ctrl+C (on Windows, SIGINT)
an error occurs (SIGABRT)
So yeah, how? Is it only possible with GUI application?

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).

Related

How could I understand that user pressed X button on cmd window?

I have written a program in C++ and before exit I store some data in files. However, if the user clicks on the close box (X) on top right corner I lose those data. Is there a way to detect if the user clicks on the close box, so that I call some functions before exit?
Use SetConsoleCtrlHandler() to installer a handler that looks for the CTRL_CLOSE_EVENT event:
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).

How is CLion terminating a process runing?

I am working on a TCP server that I'm developing on CLion under Windows 10.
I would like my program to be able to end properly, when I click on "stop" my program. I thought CLion was sending a signal, but after trying to catch them all, it looks like it does not.
So my question is, how does CLion stop running the program? Is it possible to detect it within the program?
Thank you in advance.
I found my answer here.
According to this link, signals are actually used :
Click this button to terminate the current process externally by means
of the standard shutdown script. Clicking the button once invokes soft
kill allowing the application to catch the SIGINT event and perform
graceful termination (on Windows, the Ctrl+C event is emulated). After
the button is clicked once, it is replaced with icon run tool window
kill indicating that subsequent click will lead to force termination
of the application, e.g. on Unix SIGKILL is sent.

How to make C++ to execute a function when I close the console

I want a C++ program to execute a function when I close manually the console.
I made a C++ program that test a password and if it isn't correct make the windows to log off.
But if I close the console from the "X" button nothing happens and I want to make windows to log off too if the console is closed from "X" button?
I tried _onexit_t oe() function but it doesn't help me.
So there is a method to do that or to hide the bar which contains the "Minimize", "Maximize" and "Close" buttons?
Assuming you mean the normal textual console window, you can register your own event handler via SetConsoleCtrlHandler and watch for the events CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT, etc.

Have a user close a process' allocated console without exiting the process?

Using the Win32 API in Visual C++, I want to create a program under the Windows subsystem that allocates a console with AllocConsole and writes to it with WriteConsole. However, if the user closes the console, the process should keep running in the background. As it stands, I can't get that to happen. When X is pressed on the console title bar, the process exits.
Is there any particular way of doing this?
Thanks in advance!
The key is to respond to the Console Control Event that is raised when the user attempts to close the console. You can then call FreeConsole to detach your program from the console, and let the console be destroyed. That should keep your program running.
Additional info:
If the process is terminated when the HandlerRoutine exits, then my suggestion didn't work as expected. If that's the case, then you might have a problem. You can try hooking the SC_CLOSE system message, and do the FreeConsole there before passing the message on. That might work, although I don't know what it'll do if the user presses Ctrl+C or Ctrl+Break.
The problem is that the control handler exits the process. It might be that calling FreeConsole in the HandlerRoutine is too late.

What happens when you close a c++ console application

I guess the question says it all, but, what happens if someone closes a c++ console app? As in, clicks the "x" in the top corner. Does it instantly close? Does it throw some sort of exception? Is it undefined behavior?
Closing a c++ console app with the "x" in the top corner throws an CTRL_CLOSE_EVENT which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function. In there you could override the close functionality and perform whatever you wished to do, and then optionally still perform the default behavior.
I imagine that the console process just gets unceremoniously killed by the OS. If you want to trap this event and do something it looks like the SetConsoleCtrlHandler function is the way to do it.
See also:
How to handle a ctrl-break signal in a command line interface
Console Event Handling
On Linux and other Unix systems, the console runs as a separate process. As you close the shell, it sends the SIGHUP signal to the currently active process or processes that are not executed in the background. If the programmer does not handle it, the process simply terminates. The same signal is sent if you close the SSH session with a terminal and an active process.
SIGBREAK is raised on Windows.