I have a command line application on Windows 7. It consists mainly of an endless loop. When I click on the close button of the command-line console window, it seems to be killed instantly. However, I close an SQLite database connection in the destructor, and so I am afraid that the databse might be corrupted. Even if not, I'd like to be able to properly terminate my object (maybe by writing something to a file, logging, etc.).
Can I somehow make sure that my destructor is called? What exactly happens if I close the window? Is there a "softer" way to close a program?
A console application doesn't have a message loop where you're asked to quit however Windows gives you ability to register a function to receive some notifications you may need to handle. One of them is the closing notification.
First of all declare a function that Windows will call for this purpose with prototype given by HandlerRoutine:
Now register that function, using SetControlCtrlHandler, before you start your loop:
That's done. You can explicitly delete objects you want to dispose or simply set a flag that will break your infinite loop.
Note that you will receive more events (Windows shutdown
CTRL_SHUTDOWN_EVENT
, user log-offCTRL_LOGOFF_EVENT
, user breakCTRL_C_EVENT
, closingCTRL_CLOSE_EVENT
), handle what you need/want.