_dump.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef __FC_DUMP_H__
  2. #define __FC_DUMP_H__
  3. #include <DbgHelp.h>
  4. #include <time.h>
  5. #include <atlbase.h>
  6. #pragma comment( lib, "DbgHelp.lib" )
  7. class _FCDump
  8. {
  9. protected:
  10. static LONG WINAPI _fcUnhandledExceptionFilter(
  11. _In_ struct _EXCEPTION_POINTERS *ExceptionInfo
  12. )
  13. {
  14. TCHAR appPath[MAX_PATH] = { 0 };
  15. GetModuleFileName(NULL, appPath, MAX_PATH);
  16. TCHAR* lastslot = _tcsrchr(appPath, '.');
  17. if (lastslot)
  18. *lastslot = 0;
  19. time_t tNow = time(NULL);
  20. struct tm tmNow;
  21. localtime_s(&tmNow, &tNow);
  22. TCHAR sTime[MAX_PATH] = { 0 };
  23. swprintf_s(sTime, L"%s.dmp"
  24. , appPath);
  25. HANDLE lhDumpFile = CreateFile(sTime, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  26. MINIDUMP_EXCEPTION_INFORMATION loExceptionInfo;
  27. loExceptionInfo.ExceptionPointers = ExceptionInfo;
  28. loExceptionInfo.ThreadId = GetCurrentThreadId();
  29. loExceptionInfo.ClientPointers = TRUE;
  30. MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),lhDumpFile, MiniDumpNormal, &loExceptionInfo, NULL, NULL);
  31. CloseHandle(lhDumpFile);
  32. return EXCEPTION_EXECUTE_HANDLER;
  33. }
  34. static void _fcInvalidParameterHandler( const wchar_t* expression,
  35. const wchar_t* function,
  36. const wchar_t* file,
  37. unsigned int line,
  38. uintptr_t pReserved )
  39. {
  40. char appPath[ MAX_PATH ] = {0};
  41. GetModuleFileNameA( NULL, appPath, MAX_PATH );
  42. char* lastslot = strrchr( appPath, '.' );
  43. if( lastslot )
  44. *lastslot = 0;
  45. time_t tNow = time(NULL);
  46. struct tm tmNow;
  47. localtime_s( &tmNow, &tNow );
  48. char sDumpPath[ MAX_PATH ] = {0};
  49. sprintf_s( sDumpPath, "%s.iph"
  50. , appPath );
  51. HANDLE lhDumpFile = CreateFileA(sDumpPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  52. USES_CONVERSION;
  53. char buffer[1024];
  54. DWORD nLen = 0;
  55. sprintf_s( buffer, "传入函数<%s>的参数无效.\nFile: %s Line: %d\n", W2A(function), W2A(file), line );
  56. WriteFile( lhDumpFile, buffer, strlen( buffer ), &nLen, NULL );
  57. sprintf_s( buffer, "异常描述: %s\n", W2A(expression) );
  58. WriteFile( lhDumpFile, buffer, strlen( buffer ), &nLen, NULL );
  59. CloseHandle(lhDumpFile);
  60. throw(1);
  61. }
  62. static void _fcPurecallHandler(void)
  63. {
  64. throw(1);
  65. }
  66. public:
  67. static void StartDump( void )
  68. {
  69. SetUnhandledExceptionFilter( &_fcUnhandledExceptionFilter );
  70. _set_invalid_parameter_handler( _fcInvalidParameterHandler );
  71. _set_purecall_handler( _fcPurecallHandler );
  72. _CrtSetReportMode( _CRT_ASSERT, 0 );
  73. }
  74. };
  75. #endif // !__FC_DUMP_H__