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.

138 lines
2.9 KiB

  1. #pragma once
  2. void __cdecl StructuredExceptionHandler(unsigned int u, EXCEPTION_POINTERS* pExp);
  3. int __cdecl MemoryExceptionHandler(size_t size);
  4. class CExcept
  5. {
  6. };
  7. class CAbortExcept : public CExcept
  8. {
  9. };
  10. class CMemoryHandler
  11. {
  12. public:
  13. CMemoryHandler()
  14. {
  15. pSavedHandler = _set_new_handler(
  16. MemoryExceptionHandler);
  17. }
  18. CMemoryHandler(_PNH pHandler)
  19. {
  20. pSavedHandler = _set_new_handler(
  21. pHandler);
  22. }
  23. ~CMemoryHandler()
  24. {
  25. _set_new_handler(pSavedHandler);
  26. }
  27. private:
  28. _PNH pSavedHandler;
  29. };
  30. class CExceptionHandler
  31. {
  32. public:
  33. CExceptionHandler()
  34. {
  35. pSavedHandler = _set_se_translator(
  36. StructuredExceptionHandler);
  37. }
  38. CExceptionHandler(_se_translator_function pHandler)
  39. {
  40. pSavedHandler = _set_se_translator(
  41. pHandler);
  42. }
  43. ~CExceptionHandler()
  44. {
  45. _set_se_translator(pSavedHandler);
  46. }
  47. private:
  48. _se_translator_function pSavedHandler;
  49. };
  50. class CStructuredExcept : public CExcept
  51. {
  52. public:
  53. CStructuredExcept(
  54. unsigned int ExceptionCode,
  55. PEXCEPTION_POINTERS pExceptionPointers) :
  56. m_ExceptionCode(ExceptionCode),
  57. m_pExceptionPointers(pExceptionPointers) {}
  58. unsigned int GetExceptionCode()
  59. { return m_ExceptionCode; }
  60. PEXCEPTION_RECORD GetExceptionRecord()
  61. { return m_pExceptionPointers->ExceptionRecord; }
  62. PCONTEXT GetExceptionContext()
  63. { return m_pExceptionPointers->ContextRecord; }
  64. protected:
  65. CStructuredExcept() :
  66. m_ExceptionCode(0),
  67. m_pExceptionPointers(NULL) {}
  68. unsigned int m_ExceptionCode;
  69. PEXCEPTION_POINTERS m_pExceptionPointers;
  70. };
  71. class CMemoryExcept : public CExcept
  72. {
  73. public:
  74. CMemoryExcept(DWORD dwSize) :
  75. m_dwSize(dwSize) {}
  76. DWORD GetSize()
  77. { return m_dwSize; }
  78. protected:
  79. CMemoryExcept() :
  80. m_dwSize(NULL) {}
  81. DWORD m_dwSize;
  82. };
  83. class CApiExcept : public CExcept
  84. {
  85. public:
  86. CApiExcept(DWORD dwError, PCTSTR pcszDescription) :
  87. m_dwError(dwError),
  88. m_pcszDescription(pcszDescription) {}
  89. public:
  90. PCTSTR GetDescription() { return m_pcszDescription; }
  91. DWORD GetError() { return m_dwError; }
  92. protected:
  93. DWORD m_dwError;
  94. PCTSTR m_pcszDescription;
  95. };
  96. class CWin32ApiExcept : public CApiExcept
  97. {
  98. public:
  99. CWin32ApiExcept(DWORD dwError, PCTSTR pcszDescription) :
  100. CApiExcept(dwError, pcszDescription) {}
  101. };
  102. // These macros will enable the memory depletion and se translation callback
  103. // functions. These callbacks will be enabled within the current scope. The
  104. // callbacks will be disabled when execution leaves the current scope.
  105. // Notice how the macro just instantiates an object, with the constructor and
  106. // destructor of that object doing the actual work.
  107. #define MEMORY_EXCEPTIONS() \
  108. CMemoryHandler __MemoryHandler__
  109. #define STRUCTURED_EXCEPTIONS() \
  110. CExceptionHandler __ExceptionHandler__