Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
1.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000
  6. //
  7. // File: console.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include <stdio.h>
  13. static DWORD g_dwConsoleCtrlEvent = DWORD(-1);
  14. //
  15. // Handler for console Control events.
  16. //
  17. BOOL WINAPI
  18. CtrlCHandler(
  19. DWORD dwCtrlType
  20. )
  21. {
  22. BOOL bResult = TRUE; // Assume handled.
  23. switch(dwCtrlType)
  24. {
  25. case CTRL_C_EVENT:
  26. case CTRL_BREAK_EVENT:
  27. case CTRL_CLOSE_EVENT:
  28. case CTRL_LOGOFF_EVENT:
  29. g_dwConsoleCtrlEvent = dwCtrlType;
  30. break;
  31. default:
  32. bResult = FALSE;
  33. break; // Ignore
  34. }
  35. return bResult;
  36. }
  37. //
  38. // Registers CtrlCHandler() as the console control event
  39. // handler.
  40. //
  41. HRESULT
  42. ConsoleInitialize(
  43. void
  44. )
  45. {
  46. HRESULT hr = S_OK;
  47. if (!SetConsoleCtrlHandler(CtrlCHandler, TRUE))
  48. {
  49. const DWORD dwErr = GetLastError();
  50. hr = HRESULT_FROM_WIN32(dwErr);
  51. }
  52. return hr;
  53. }
  54. //
  55. // Unregisters CtrlCHandler() as the console control event
  56. // handler.
  57. //
  58. HRESULT
  59. ConsoleUninitialize(
  60. void
  61. )
  62. {
  63. HRESULT hr = S_OK;
  64. if (!SetConsoleCtrlHandler(CtrlCHandler, FALSE))
  65. {
  66. const DWORD dwErr = GetLastError();
  67. hr = HRESULT_FROM_WIN32(dwErr);
  68. }
  69. return hr;
  70. }
  71. //
  72. // Determines if a console control event has occured.
  73. // Optionally returns the event code.
  74. //
  75. BOOL
  76. ConsoleHasCtrlEventOccured(
  77. DWORD *pdwCtrlEvent // [optional]. Default is NULL.
  78. )
  79. {
  80. BOOL bResult = FALSE;
  81. if (DWORD(-1) != g_dwConsoleCtrlEvent)
  82. {
  83. bResult = TRUE;
  84. if (NULL != pdwCtrlEvent)
  85. {
  86. *pdwCtrlEvent = g_dwConsoleCtrlEvent;
  87. }
  88. }
  89. return bResult;
  90. }