0

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?

image of cmd

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • 1
    Is this on Windows? If so, take a look at [`SetConsoleCtrlHandler`](https://learn.microsoft.com/en-us/windows/console/setconsolectrlhandler) – Paul Sanders Sep 27 '20 at 22:55

1 Answers1

2

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770