i have a list class that i need to save upon exit i cant figure out a way to do this using this method can make this method a mwmber of the class or pass the object with it?
void main()
{
if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
{
// unable to install handler...
// display message to the user
printf("Unable to install handler!\n")
Why do you need a console control handler? Can't you do it in the destructor of a class? Sounds simpler to me.
1 2 3 4 5 6 7 8 9 10 11 12 13
class GoOnExit
{
~GoOnExit()
{
//Put your code here.
}
}
int main()
{
GoOnExit __willDoOnExit;
//Now no matter how the program exits, the destructor of __willDoOnExit will execute.
}
BOOL WINAPI ConsoleHandler(DWORD dwCtrlEvent)
{
switch (dwCtrlEvent)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
{
std::ofstream os("appstate.save");
os << gAppState << std::endl; // Assume the state's in global gAppState
return TRUE;
}
case CTRL_SHUTDOWN_EVENT:
default:
return FALSE;
}
}