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