The question is not completely clear, so sorry if I'm addressing a wrong problem…
This is not a Windowed application. When a windows is closed, it's already too late.
Instead, you can intercept
Ctrl+C
in your console application to perform post-mortal actions such as proper clean-up.
See
http://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c[
^].
—SA