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