C++生成windows转储文件

C++生成windows转储文件,代码如下:

#include <Windows.h>
#include <Dbghelp.h>

void MakeMinidump(EXCEPTION_POINTERS* e) {
       auto hDbgHelp = LoadLibraryA("dbghelp");
       if(hDbgHelp == nullptr)
           return;
       auto pMiniDumpWriteDump = (decltype(&MiniDumpWriteDump))GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
       if(pMiniDumpWriteDump == nullptr)
           return;

       char name[MAX_PATH];
       {
           auto nameEnd = name + GetModuleFileNameA(GetModuleHandleA(0), name, MAX_PATH);
           SYSTEMTIME t;
           GetSystemTime(&t);
           wsprintfA(nameEnd - strlen(".exe"),
               "_%4d%02d%02d_%02d%02d%02d.dmp",
               t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);
       }

       auto hFile = CreateFileA(name, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
       if(hFile == INVALID_HANDLE_VALUE)
           return;

       MINIDUMP_EXCEPTION_INFORMATION exceptionInfo;
       exceptionInfo.ThreadId = GetCurrentThreadId();
       exceptionInfo.ExceptionPointers = e;
       exceptionInfo.ClientPointers = FALSE;

       auto dumped = pMiniDumpWriteDump(
           GetCurrentProcess(),
           GetCurrentProcessId(),
           hFile,
           MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory),
           e ? &exceptionInfo : nullptr,
           nullptr,
           nullptr);

       CloseHandle(hFile);

       return;
}

LONG CALLBACK UnhandledHandler(EXCEPTION_POINTERS* e) {
       MakeMinidump(e);
       return EXCEPTION_CONTINUE_SEARCH;
}

int main() {
       SetUnhandledExceptionFilter(UnhandledHandler);
       return 0;
}


本文为@艺文笔记原创文章,转载请注明出处:https://www.xuwenyan.com/archives/145,非常感谢!
上一篇:  下一篇: