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