Leaked source code of windows server 2003
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.

140 lines
3.2 KiB

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