Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing DOS window , closes the application.

Status
Not open for further replies.

shirukhan

MIS
Oct 11, 2002
3
US
Problem: The c++ applicaton has code for graceful shutdown. But whenever the user closes the DOS window, the application crashes. I have tried adding exception handling (set_terminate etc.) but with no success. The application is an exe file and prints output to the dos window.

Does anybody know how to catch the event when DOS window is closed (using 'X' window closing button or on CTRL-C) so that I can execute the shutdown code?
 
Have a look at this function :

SetConsoleCtrlHandler()

/JOlesen
 
Thanks. If you have an example that would be help me a lot.
 
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;



BOOL WINAPI XHandler (DWORD what)
{
ofstream whatHappened(&quot;WhatHappened.txt&quot;);

switch (what)
{
case CTRL_C_EVENT:
/* A CTRL+c signal was received, either from keyboard input or
from a signal generated by the GenerateConsoleCtrlEvent
function.
*/
whatHappened << &quot;Got Ctrl-Ced&quot; << endl;
break;

case CTRL_BREAK_EVENT:
/* A CTRL+BREAK signal was received, either from keyboard input or
from a signal generated by GenerateConsoleCtrlEvent.
*/
whatHappened << &quot;Got broken&quot; << endl;
break;

case CTRL_CLOSE_EVENT:
/* A signal that the system sends to all processes attached to a
console when the user closes the console (either by choosing the
Close command from the console window's System menu, or by
choosing the End Task command from the Task List).
*/
whatHappened << &quot;Got closed&quot; << endl;
break;

case CTRL_LOGOFF_EVENT:
/* A signal that the system sends to all console processes when
a user is logging off. This signal does not indicate which user
is logging off, so no assumptions can be made.
*/
whatHappened << &quot;Got logged off&quot; << endl;
break;

case CTRL_SHUTDOWN_EVENT:
/* A signal that the system sends to all console processes when
the system is shutting down.
*/
whatHappened << &quot;Got shut down&quot; << endl;
break;
}

/* If true is returned, a dialog box indicating that the process
needs more time to terminate will pop up
*/
return FALSE;
}


int main ()
{
int cupid;

SetConsoleCtrlHandler (XHandler, true);
// Wait for something to happen
cin >> cupid;

return 0;
}
 
Thanks a lot, I appreciate it. I will give this solution a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top