1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// ConsoleClose.cpp 2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h" 4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "ConsoleClose.h" 6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 7baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic int g_BreakCounter = 0; 8baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const int kBreakAbortThreshold = 2; 9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 10baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NConsoleClose { 11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#if !defined(UNDER_CE) && defined(_WIN32) 13baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic BOOL WINAPI HandlerRoutine(DWORD ctrlType) 14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (ctrlType == CTRL_LOGOFF_EVENT) 16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync // printf("\nCTRL_LOGOFF_EVENT\n"); 18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return TRUE; 19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync g_BreakCounter++; 22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (g_BreakCounter < kBreakAbortThreshold) 23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return TRUE; 24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return FALSE; 25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /* 26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync switch(ctrlType) 27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case CTRL_C_EVENT: 29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case CTRL_BREAK_EVENT: 30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (g_BreakCounter < kBreakAbortThreshold) 31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return TRUE; 32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return FALSE; 34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync */ 35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif 37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 38baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool TestBreakSignal() 39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #ifdef UNDER_CE 41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return false; 42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #else 43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /* 44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (g_BreakCounter > 0) 45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return true; 46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync */ 47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return (g_BreakCounter > 0); 48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #endif 49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 51baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CheckCtrlBreak() 52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (TestBreakSignal()) 54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync throw CCtrlBreakException(); 55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 57baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCCtrlHandlerSetter::CCtrlHandlerSetter() 58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #if !defined(UNDER_CE) && defined(_WIN32) 60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if(!SetConsoleCtrlHandler(HandlerRoutine, TRUE)) 61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync throw "SetConsoleCtrlHandler fails"; 62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #endif 63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 65baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCCtrlHandlerSetter::~CCtrlHandlerSetter() 66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #if !defined(UNDER_CE) && defined(_WIN32) 68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if(!SetConsoleCtrlHandler(HandlerRoutine, FALSE)) 69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync throw "SetConsoleCtrlHandler fails"; 70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #endif 71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 74