简写版本
1.minidump.h
minidump.h
#include <stdio.h>
#include <windows.h>
#include <dbghelp.h>
#pragma comment(lib, "Dbghelp.lib")
LONG WINAPI MyUnhandledFilter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
LONG ret = EXCEPTION_EXECUTE_HANDLER;
TCHAR szFileName[64];
SYSTEMTIME st;
::GetLocalTime(&st);
wsprintf(szFileName, TEXT("%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 = ::CreateFile(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)
{
printf("Create Dump File Success!\n");
}
else
{
printf("MiniDumpWriteDump Failed: %d\n", GetLastError());
}
::CloseHandle(hFile);
}
else
{
printf("Create File %s Failed %d\n", szFileName, GetLastError());
}
return ret;
}
#include <windows.h>
#include <dbghelp.h>
#pragma comment(lib, "Dbghelp.lib")
LONG WINAPI MyUnhandledFilter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
LONG ret = EXCEPTION_EXECUTE_HANDLER;
TCHAR szFileName[64];
SYSTEMTIME st;
::GetLocalTime(&st);
wsprintf(szFileName, TEXT("%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 = ::CreateFile(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)
{
printf("Create Dump File Success!\n");
}
else
{
printf("MiniDumpWriteDump Failed: %d\n", GetLastError());
}
::CloseHandle(hFile);
}
else
{
printf("Create File %s Failed %d\n", szFileName, GetLastError());
}
return ret;
}
2.调用
int main()
{
::SetUnhandledExceptionFilter(MyUnhandledFilter);
int *p;
*p = 100;
return 0;
}
{
::SetUnhandledExceptionFilter(MyUnhandledFilter);
int *p;
*p = 100;
return 0;
}
3.调试dump文件
把dmp文件和exe, pdb文件放在同一目录下, 然后用编译器(如vc)打开, 然后开始调试就会中断到刚才中断的地方.
from:http://hi.baidu.com/lifezfx/blog/item/4d925eef2dcc65eace1b3ef0.html