Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when you close a c++ console application

Tags:

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?

like image 288
rlbond Avatar asked Mar 30 '09 05:03

rlbond


People also ask

How do I exit AC console application?

You can use Environment. Exit(0) and Application. Exit .

How do I shutdown the console in C#?

Exit(0); Or to close the current instance of the form: this. Close();

What is AC console application?

A console application, in the context of C#, is an application that takes input and displays output at a command line console with access to three basic data streams: standard input, standard output and standard error.


2 Answers

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.

like image 133
Raul Agrait Avatar answered Oct 02 '22 15:10

Raul Agrait


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
like image 39
AndrewR Avatar answered Oct 02 '22 13:10

AndrewR