天天看点

使用SetUnhandledExceptionFilter转储程序崩溃时内存DMP注意事项

SetUnhandledExceptionFilter

部分源码:

#include "stdafx.h"

#include "DumpHandle.h"  

#include <stdlib.h>

#include <stdio.h>

#include <dbghelp.h>

#pragma comment(lib, "Dbghelp.lib")

LONG WINAPI DumpHandleFilterA(struct _EXCEPTION_POINTERS *lpExceptionInfo)

{

LONG ret = EXCEPTION_EXECUTE_HANDLER;

char szFileName[128] = {0};

SYSTEMTIME st = {0};

::GetLocalTime(&st); 

wsprintfA(szFileName, ("CrashAt[%04d-%02d-%02d-%02d-%02d-%02d-%02d-%02d].dmp"), 

st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, rand()%100);

HANDLE hFile = ::CreateFileA(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );

if (hFile != INVALID_HANDLE_VALUE)

MINIDUMP_EXCEPTION_INFORMATION ExInfo;         

ExInfo.ThreadId = ::GetCurrentThreadId();       

ExInfo.ExceptionPointers = lpExceptionInfo;        

ExInfo.ClientPointers = false;       

// write the dump        

BOOL bOK = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL );         

if (bOK)

DumpDbgPrintA(("Create Dump File Success!/n")); 

DumpDbgMsgboxA("Create Dump File Success!/n");

}

else

DumpDbgPrintA(("MiniDumpWriteDump Failed: %d/n"), GetLastError() ); 

DumpDbgMsgboxA(("MiniDumpWriteDump Failed: %d/n"), GetLastError() );

::CloseHandle(hFile);

DumpDbgPrintA(("Create File %s Failed %d/n"), szFileName, GetLastError());

DumpDbgMsgboxA(("Create File %s Failed %d/n"), szFileName, GetLastError()); 

return ret;

void DumpDbgPrintA (char *fmt, ... )

va_list argptr;/* Argument list pointer*/

char str[1024*3] = {0};/* Buffer to build sting into*/

va_start (argptr, fmt);/* Initialize va_ functions*/

wvsprintfA (str, fmt, argptr);/* prints string to buffer*/

OutputDebugStringA(str);

va_end (argptr);/* Close va_ functions*/

void DumpDbgMsgboxA (char *fmt, ... )

MessageBoxA (NULL, str, "Debug Message", 

MB_ICONINFORMATION | MB_OK);

继续阅读