windows设置程序崩溃转储

设置崩溃处理函数

#if (defined _WIN32)||(defined _WIN64)
#include <windows.h>
#include <Dbghelp.h>
#include <tchar.h>
//#pragma execution_character_set( "utf-8" )
typedef BOOL(WINAPI* MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType, CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);

void CreateMinDump(struct _EXCEPTION_POINTERS* apExceptionInfo)
{
    HMODULE mhLib = ::LoadLibrary(_T("dbghelp.dll"));
    MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(mhLib, "MiniDumpWriteDump");

    HANDLE  hFile = ::CreateFile(_T("core.dmp"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);

    _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
    ExInfo.ThreadId = ::GetCurrentThreadId();
    ExInfo.ExceptionPointers = apExceptionInfo;
    ExInfo.ClientPointers = FALSE;

    pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL);
    ::CloseHandle(hFile);
}

long   __stdcall   HandlerCrash(struct _EXCEPTION_POINTERS* apExceptionInfo)
{
    DLOG(INFO) << "crashed!!!!!!!!!!!!!!!!!!!!!!!!!";
    CreateMinDump(apExceptionInfo);
    return EXCEPTION_CONTINUE_SEARCH;
}
#endif

注册崩溃事件

#if (defined _WIN32)||(defined _WIN64)
    SetUnhandledExceptionFilter(HandlerCrash);
#endif
MINIDUMPWRITEDUMP 不是线程安全的 有时可能无法生成转储文件


通过配置注册表windows错误报告时限捕捉转储文件 wer

reg add "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsWindows Error ReportingLocalDumps" /f
reg add "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsWindows Error ReportingLocalDumpshttpd.exe" /f
reg add "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsWindows Error ReportingLocalDumpshttpd.exe" /v DumpFolder /t REG_EXPAND_SZ /d "C:crashdumps" /f
reg add "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsWindows Error ReportingLocalDumpshttpd.exe" /v DumpType /t REG_DWORD /d "1" /f

其中

DumpType 设置1 未mindump   转储文件几m
      设置为2 为fulldump 转储文件几十m


原文地址:https://www.cnblogs.com/wolbo/p/14868051.html