CrashDumper.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <tchar.h>
  4. #include <dbghelp.h>
  5. #include <string>
  6. #include "CrashDumper.h"
  7. #pragma comment(lib, "dbghelp.lib")
  8. //CrashDumper dumper;
  9. CrashDumper::CrashDumper()
  10. {
  11. m_OriginalFilter = SetUnhandledExceptionFilter(ExceptionFilter);
  12. }
  13. CrashDumper::~CrashDumper()
  14. {
  15. SetUnhandledExceptionFilter(m_OriginalFilter);
  16. }
  17. LONG WINAPI CrashDumper::ExceptionFilter(struct _EXCEPTION_POINTERS* ExceptionInfo)
  18. {
  19. bool bDumpOK = false;
  20. DWORD dwProcess = GetCurrentProcessId();
  21. HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcess);
  22. if (hProcess != INVALID_HANDLE_VALUE)
  23. {
  24. TCHAR szPath[MAX_PATH];
  25. if (GetModuleFileName(NULL, szPath, sizeof(szPath)))
  26. {
  27. wstring strDumpFileName = szPath;
  28. strDumpFileName += TEXT(".dmp");
  29. HANDLE hFile = CreateFile(strDumpFileName.c_str(), FILE_ALL_ACCESS, 0, NULL, CREATE_ALWAYS, NULL, NULL);
  30. if (hFile != INVALID_HANDLE_VALUE)
  31. {
  32. MINIDUMP_EXCEPTION_INFORMATION exception_information;
  33. exception_information.ThreadId = GetCurrentThreadId();
  34. exception_information.ExceptionPointers = ExceptionInfo;
  35. exception_information.ClientPointers = TRUE;
  36. if (MiniDumpWriteDump(hProcess, dwProcess, hFile, MiniDumpNormal, &exception_information, NULL, NULL))
  37. {
  38. bDumpOK = true;
  39. }
  40. CloseHandle(hFile);
  41. }
  42. }
  43. CloseHandle(hProcess);
  44. }
  45. //if (bDumpOK)
  46. // MessageBox(NULL, TEXT("本程序遇到未处理的异常,MiniDump文件已经生成在程序的运行目录。"), TEXT("提示"), MB_OK);
  47. //else
  48. // MessageBox(NULL, TEXT("本程序遇到未处理的异常,生成MiniDump文件失败。"), TEXT("提示"), MB_OK);
  49. return EXCEPTION_EXECUTE_HANDLER;
  50. }
  51. bool CrashDumper::_PlaceHolder() { return true; }