dllmain.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <Windows.h>
  2. #include <dbghelp.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. static void dump_file(const TCHAR *path, EXCEPTION_POINTERS *exception)
  6. {
  7. HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  8. MINIDUMP_EXCEPTION_INFORMATION dump;
  9. dump.ExceptionPointers = exception;
  10. dump.ThreadId = GetCurrentThreadId();
  11. dump.ClientPointers = TRUE;
  12. MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithFullMemory, &dump, NULL, NULL);
  13. CloseHandle(file);
  14. }
  15. #if _MSC_VER >= 1300 // for VC 7.0
  16. // from ATL 7.0 sources
  17. #ifndef _delayimp_h
  18. extern "C" IMAGE_DOS_HEADER __ImageBase;
  19. #endif
  20. #endif
  21. static HMODULE get_current_module()
  22. {
  23. #if _MSC_VER < 1300 // earlier than .NET compiler (VC 6.0)
  24. // Here's a trick that will get you the handle of the module
  25. // you're running in without any a-priori knowledge:
  26. // http://www.dotnet247.com/247reference/msgs/13/65259.aspx
  27. MEMORY_BASIC_INFORMATION mbi;
  28. static int dummy;
  29. VirtualQuery(&dummy, &mbi, sizeof(mbi));
  30. return reinterpret_cast<HMODULE>(mbi.AllocationBase);
  31. #else // VC 7.0
  32. // from ATL 7.0 sources
  33. return reinterpret_cast<HMODULE>(&__ImageBase);
  34. #endif
  35. }
  36. static long exception_handler(EXCEPTION_POINTERS *ep)
  37. {
  38. char dmp_path[MAX_PATH] = { 0 };
  39. char temp_path[MAX_PATH] = { 0 };
  40. //c://users//appdata//local//temp//recorder.dmp
  41. if (GetTempPath(MAX_PATH, temp_path)) {
  42. sprintf_s(dmp_path, MAX_PATH, "%srecorder.dmp", temp_path);
  43. printf("%s\r\n", dmp_path);
  44. }
  45. dump_file(dmp_path, ep);
  46. return EXCEPTION_EXECUTE_HANDLER;
  47. }
  48. bool APIENTRY DllMain(HMODULE hModule,
  49. DWORD ul_reason_for_call,
  50. LPVOID lpReserved
  51. )
  52. {
  53. switch (ul_reason_for_call)
  54. {
  55. case DLL_PROCESS_ATTACH:
  56. SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)exception_handler);
  57. break;
  58. case DLL_THREAD_ATTACH:
  59. break;
  60. case DLL_THREAD_DETACH:
  61. case DLL_PROCESS_DETACH:
  62. break;
  63. }
  64. return TRUE;
  65. }