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.

96 lines
3.0 KiB

  1. #include <windows.h>
  2. #include "errorrep.h"
  3. // This is the exception filter that we will use to catch an otherwise
  4. // unhandled exceptions (if a global unhandled exception filter is used)
  5. // or to catch exceptions that occur in a __try / __except block.
  6. LONG MyExceptionFilter(EXCEPTION_POINTERS *pep)
  7. {
  8. EFaultRepRetVal frrv;
  9. pfn_REPORTFAULT pfn = NULL;
  10. HMODULE hmodFaultRep = NULL;
  11. LONG lRet = EXCEPTION_CONTINUE_SEARCH;
  12. // Manually load the error reporting dll. We could have called ReportFault
  13. // directly and implicitly linked with the dll, but unexpected exceptions
  14. // should not be a common scenerio so we will save resources and only load
  15. // it when necessary.
  16. // In addition, since this feature is only available on Windows Whistler,
  17. // this will let an application continue to function when on an earlier
  18. // version of Windows.
  19. hmodFaultRep = LoadLibrary("faultrep.dll");
  20. if (hmodFaultRep != NULL)
  21. {
  22. lRet = EXCEPTION_EXECUTE_HANDLER;
  23. pfn = (pfn_REPORTFAULT)GetProcAddress(hmodFaultRep, "ReportFault");
  24. if (pfn != NULL)
  25. frrv = (*pfn)(pep, 0);
  26. else
  27. lRet = EXCEPTION_CONTINUE_SEARCH;
  28. FreeLibrary(hmodFaultRep);
  29. }
  30. return lRet;
  31. };
  32. int __cdecl main(int argc, char **argv)
  33. {
  34. DWORD *pdw = NULL;
  35. BOOL fUseGlobalExceptionFilter = FALSE;
  36. // There are two ways to handle unexpected exception in applications.
  37. // Either
  38. // define a global unhandled exception filter that will get called
  39. // if nothing else handles the exception.
  40. // use a __try / __except block to catch exceptions and define your
  41. // own filter function.
  42. // Both methods will be demonstated below. If the user of this app passed
  43. // "UseGlobalFilter" as an argument to this test application, we will
  44. // use a global unhandled exception filter. Otherwise, we will use a
  45. // __try / __except block.
  46. // Use a global unhandled exception filter
  47. if (argc > 1 && _stricmp(argv[1], "UseGlobalFilter") == 0)
  48. {
  49. // Set the global unhandled exception filter to the exception filter
  50. // function we defined above.
  51. SetUnhandledExceptionFilter(MyExceptionFilter);
  52. // cause a fault
  53. *pdw = 1;
  54. }
  55. // Use __try / __except blocks
  56. else
  57. {
  58. __try
  59. {
  60. // cause a fault
  61. *pdw = 1;
  62. }
  63. // we need to pass the structure returned by GetExceptionInformation()
  64. // to the filter.
  65. // Note that the pointer returned by this function is only valid during
  66. // the execution of the filter (that is, within the parenthesis of the
  67. // __except() statement). If it needs to be saved, you must copy the
  68. // contents of the structure somewhere else.
  69. __except(MyExceptionFilter(GetExceptionInformation()))
  70. {
  71. // we don't do anything interesting in this handler
  72. }
  73. }
  74. return 0;
  75. }