C++ 代碼: 新建一個WIN32 工程, 加入以下代碼就行
#include "stdafx.h"
#include <windows.h>
#include <dbghelp.h>
#include <stdio.h>
#include <time.h>
#pragma comment(lib, "dbghelp")
#ifndef _M_IX86
#error "The following code only works for x86!"
#endif
void DisableSetUnhandledExceptionFilter()
{
void *addr = (void*)GetProcAddress(LoadLibrary(_T("kernel32.dll")),
"SetUnhandledExceptionFilter");
if (addr)
{
unsigned char code[16];
int size = 0;
code[size++] = 0x33;
code[size++] = 0xC0;
code[size++] = 0xC2;
code[size++] = 0x04;
code[size++] = 0x00;
DWORD dwOldFlag, dwTempFlag;
VirtualProtect(addr, size, PAGE_READWRITE, &dwOldFlag);
WriteProcessMemory(GetCurrentProcess(), addr, code, size, NULL);
VirtualProtect(addr, size, dwOldFlag, &dwTempFlag);
}
}
LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* lpExceptionInfo)
{
TCHAR szProgramPath[MAX_PATH] = {0};
if(GetModuleFileName(NULL, szProgramPath, MAX_PATH))
{
LPTSTR lpSlash = _tcsrchr(szProgramPath, '//');
if(lpSlash)
{
*(lpSlash + 1) = '/0';
}
}
TCHAR szDumpFile[MAX_PATH] = {0};
_stprintf(szDumpFile, _T("%s%d.dmp"), szProgramPath, time(NULL));
TCHAR szReportFile[MAX_PATH] = {0};
_stprintf(szReportFile, _T("%sBugReport.exe"), szProgramPath);
HANDLE hDumpFile = CreateFile(szDumpFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL ,NULL);
MINIDUMP_EXCEPTION_INFORMATION stMiniDumpExceptionInfo;
stMiniDumpExceptionInfo.ExceptionPointers = lpExceptionInfo;
stMiniDumpExceptionInfo.ThreadId = GetCurrentThreadId();
stMiniDumpExceptionInfo.ClientPointers = TRUE;
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile,
MiniDumpNormal, &stMiniDumpExceptionInfo, NULL, NULL);
CloseHandle(hDumpFile);
::ShellExecute(NULL, NULL, szReportFile, szDumpFile, NULL, SW_HIDE);
//Use your own user-friendly ui prompt here
MessageBox(NULL, _T("Oh, No! 程序居然崩潰了!"), _T("ERROR"), MB_OK);
return EXCEPTION_EXECUTE_HANDLER;
}
int main()
{
int *p = NULL;
SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
DisableSetUnhandledExceptionFilter();
*p = 22;
return 0;
}
C代碼, 新建一個空工程, 加入以下代碼
#include <windows.h>
#include <dbghelp.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
#include <TCHAR.H >
#pragma comment(lib, "dbghelp")
#ifndef _M_IX86
#error "The following code only works for x86!"
#endif
void DisableSetUnhandledExceptionFilter()
{
DWORD dwOldFlag, dwTempFlag;
void *addr = (void*)GetProcAddress(LoadLibrary(_T("kernel32.dll")),
"SetUnhandledExceptionFilter");
if (addr)
{
unsigned char code[16];
int size = 0;
code[size++] = 0x33;
code[size++] = 0xC0;
code[size++] = 0xC2;
code[size++] = 0x04;
code[size++] = 0x00;
VirtualProtect(addr, size, PAGE_READWRITE, &dwOldFlag);
WriteProcessMemory(GetCurrentProcess(), addr, code, size, NULL);
VirtualProtect(addr, size, dwOldFlag, &dwTempFlag);
}
}
LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* lpExceptionInfo)
{
TCHAR szProgramPath[MAX_PATH] = {0};
TCHAR szDumpFile[MAX_PATH] = {0};
TCHAR szReportFile[MAX_PATH] = {0};
HANDLE hDumpFile;
MINIDUMP_EXCEPTION_INFORMATION stMiniDumpExceptionInfo;
if(GetModuleFileName(NULL, szProgramPath, MAX_PATH))
{
LPTSTR lpSlash = _tcsrchr(szProgramPath, '//');
if(lpSlash)
{
*(lpSlash + 1) = '/0';
}
}
_stprintf(szDumpFile, _T("%s%d.dmp"), szProgramPath, time(NULL));
_stprintf(szReportFile, _T("%sBugReport.exe"), szProgramPath);
hDumpFile = CreateFile(szDumpFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL ,NULL);
stMiniDumpExceptionInfo.ExceptionPointers = lpExceptionInfo;
stMiniDumpExceptionInfo.ThreadId = GetCurrentThreadId();
stMiniDumpExceptionInfo.ClientPointers = TRUE;
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile,
MiniDumpNormal, &stMiniDumpExceptionInfo, NULL, NULL);
CloseHandle(hDumpFile);
ShellExecute(NULL, NULL, szReportFile, szDumpFile, NULL, SW_HIDE);
//Use your own user-friendly ui prompt here
MessageBox(NULL, _T("Oh, No! 程序居然崩潰了!"), _T("ERROR"), MB_OK);
return EXCEPTION_EXECUTE_HANDLER;
}
int main()
{
int *p = NULL;
SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
DisableSetUnhandledExceptionFilter();
*p = 22;
return 0;
}