• C++ 程序崩溃时生成Dump文件


    本方法仅限于windows平台下,跨平台不可用。

    路径需要为全路径,默认生成在当前程序的执行目录。

    注意路径的字符类型。

    #include <DbgHelp.h>
    
    //生产DUMP文件
    int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExceptionPointers, PWCHAR pwAppName)
    {
        BOOL bOwnDumpFile = FALSE;
        HANDLE hDumpFile = hFile;
        MINIDUMP_EXCEPTION_INFORMATION ExpParam;
    
        typedef BOOL(WINAPI * MiniDumpWriteDumpT)(
            HANDLE,
            DWORD,
            HANDLE,
            MINIDUMP_TYPE,
            PMINIDUMP_EXCEPTION_INFORMATION,
            PMINIDUMP_USER_STREAM_INFORMATION,
            PMINIDUMP_CALLBACK_INFORMATION
            );
    
        MiniDumpWriteDumpT pfnMiniDumpWriteDump = NULL;
        HMODULE hDbgHelp = LoadLibrary(L"DbgHelp.dll");
        if (hDbgHelp)
            pfnMiniDumpWriteDump = (MiniDumpWriteDumpT)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
    
        if (pfnMiniDumpWriteDump)
        {
            if (hDumpFile == NULL || hDumpFile == INVALID_HANDLE_VALUE)
            {
                TCHAR szFileName[MAX_PATH] = { 0 };
                TCHAR* szVersion = L"v1.0";
                TCHAR dwBufferSize = MAX_PATH;
                SYSTEMTIME stLocalTime;
                GetLocalTime(&stLocalTime);
                CreateDirectory(szFileName, NULL);
    
                wsprintf(szFileName, L"%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
                    szVersion,
                    stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
                    stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
                    GetCurrentProcessId(), GetCurrentThreadId());
           // szFileName是全路径文件名,这样可以指定dump文件的保存位置 hDumpFile
    = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0); bOwnDumpFile = TRUE; OutputDebugString(szFileName); } if (hDumpFile != INVALID_HANDLE_VALUE) { ExpParam.ThreadId = GetCurrentThreadId(); ExpParam.ExceptionPointers = pExceptionPointers; ExpParam.ClientPointers = FALSE; pfnMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithDataSegs, (pExceptionPointers ? &ExpParam : NULL), NULL, NULL); if (bOwnDumpFile) CloseHandle(hDumpFile); } } if (hDbgHelp != NULL) FreeLibrary(hDbgHelp); return EXCEPTION_EXECUTE_HANDLER; } LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo) { if (IsDebuggerPresent()) { return EXCEPTION_CONTINUE_SEARCH; } return GenerateMiniDump(NULL, lpExceptionInfo, L"test"); } int main() { //dump文件功能 SetUnhandledExceptionFilter(ExceptionFilter); }
  • 相关阅读:
    如何使用谷歌的网页删除请求工具?
    已有记录表添加特定排序主键ID
    用ASP实现超长内容的word模板替换objDoc.Content.Find.Execute
    内网SMTP发送失败的曲线救国之策
    IIS无法在后台生成WORD文件的故障
    WINDOWS2003进行WindowsFTP设置的一些注意事项
    解决IISASP调用XmlHTTP出现msxml3.dll (0x80070005) 拒绝访问的错误
    [转]Cate:我是如何准备Google面试的
    Ubuntu的启动配置文件grub.cfg(menu.lst)设置指南
    Linux启动过程详解
  • 原文地址:https://www.cnblogs.com/amwuau/p/8871737.html
Copyright © 2020-2023  润新知