1 #pragma once 2 #include <windows.h> 3 #include <imagehlp.h> 4 #include <stdlib.h> 5 #include <iostream> 6 #pragma comment(lib, "dbghelp.lib") 7 8 9 inline BOOL IsDataSectionNeeded(const WCHAR* pModuleName)10 11 {12 if(pModuleName == 0)13 {14 return FALSE;15 } 16 17 WCHAR szFileName[_MAX_FNAME] = L"";18 _wsplitpath(pModuleName, NULL, NULL, szFileName, NULL); 19 20 if(wcsicmp(szFileName, L"ntdll") == 0)21 return TRUE;22 return FALSE; 23 }24 25 26 27 inline BOOL CALLBACK MiniDumpCallback(PVOID pParam,const PMINIDUMP_CALLBACK_INPUT pInput,PMINIDUMP_CALLBACK_OUTPUT pOutput)28 29 {30 if(pInput == 0 || pOutput == 0)31 return FALSE; 32 33 switch(pInput->CallbackType)34 {35 case ModuleCallback: 36 if(pOutput->ModuleWriteFlags & ModuleWriteDataSeg) 37 if(!IsDataSectionNeeded(pInput->Module.FullPath)) 38 pOutput->ModuleWriteFlags &= (~ModuleWriteDataSeg); 39 case IncludeModuleCallback:40 case IncludeThreadCallback:41 case ThreadCallback:42 case ThreadExCallback:43 return TRUE;44 default:;45 }46 return FALSE;47 }48 49 50 51 inline void CreateMiniDump(EXCEPTION_POINTERS* pep, LPCTSTR strFileName)52 {53 HANDLE hFile = CreateFile(strFileName, GENERIC_READ | GENERIC_WRITE,54 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 55 56 if((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))57 {58 MINIDUMP_EXCEPTION_INFORMATION mdei;59 mdei.ThreadId = GetCurrentThreadId();60 mdei.ExceptionPointers = pep;61 mdei.ClientPointers = FALSE;62 MINIDUMP_CALLBACK_INFORMATION mci;63 mci.CallbackRoutine = (MINIDUMP_CALLBACK_ROUTINE)MiniDumpCallback;64 mci.CallbackParam = 0;65 MINIDUMP_TYPE mdt = (MINIDUMP_TYPE)(MiniDumpWithPrivateReadWriteMemory | 66 MiniDumpWithDataSegs | 67 MiniDumpWithHandleData |68 0x00000800 /*MiniDumpWithFullMemoryInfo*/ | 69 0x00001000 /*MiniDumpWithThreadInfo*/ | 70 MiniDumpWithUnloadedModules);71 MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),72 hFile, mdt, (pep != 0) ? &mdei : 0, 0, &mci);73 CloseHandle(hFile); 74 }75 }
Create a new header file and paste the above content into the file
1 STD: ostream & operator <(STD: ostream & OS, const exception_record & red) 2 {3 return OS <"thread ID:" <getcurrentthreadid () 4 <"exceptioncode:" <red. predictioncode <"\ n" 5 <"predictionflags:" <red. exceptionflags <"\ n" 6 <"exceptionaddress:" <red. predictionaddress <"\ n" 7 <"numberparameters:" <red. numberparameters; 8} 9 long winapi gptunhandledexceptionfilter (pexception_pointers pexceptioninfo) 10 {11 // Save the on-site file 12 createminidump (pexceptioninfo, "testdmp. DMP "); 13 cerr <" Unknown error: "<(* pexceptioninfo-> exceptionrecord) <Endl; 14 exit (pexceptioninfo-> exceptionrecord-> exceptioncode ); 15 return exception_execute_handler; // The program stops running 16}
Add the above content to the CPP File
// Register and generate the DMP file function. This code should be placed before the code that may contain exceptions.
Setunhandledexceptionfilter (gptunhandledexceptionfilter );
Program DMP file generation