/////////////////////////////////////////////////////////////
//
// File: BRExceptionFilter.cpp
// Author: Blood’s Rust
// Date: 2012.03.20
// Version: 1.0.0.0
//
/////////////////////////////////////////////////////////////
//#include “stdafx.h”
#include “windows.h”
#include “windef.h”
#include “stdio.h”
#include “tchar.h”
#include “BRExceptionFilter.h”
#include “RXLogCommon.h”
#include “DataCenter.h”
// Declarations
#ifndef _UNICODE
extern “C” int WinMainCRTStartup(void);
#else
extern “C” int wWinMainCRTStartup(void);
#endif
void DisableSetUnhandledExceptionFilter()
{
HINSTANCE hDll = LoadLibrary(_T("kernel32.dll"));
if(hDll)
{
void *addr = (void*)GetProcAddress(hDll, "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 __stdcall BRExceptionFilterWindows(_EXCEPTION_POINTERS* pExceptionInfo)
{
return BRExceptionFilter(pExceptionInfo);
}
long BRExceptionFilter(_EXCEPTION_POINTERS* pExceptionInfo)
{
HINSTANCE hDll = LoadLibrary(_T(“Dbghelp.dll”));
if(hDll)
{
FNDUMPWRITEDUMP pfnMiniDumpWriteDump = NULL;
pfnMiniDumpWriteDump = (FNDUMPWRITEDUMP)GetProcAddress(hDll, “MiniDumpWriteDump”);
if(pfnMiniDumpWriteDump)
{
TCHAR szFile[MAX_PATH] = {0};
::GetModuleFileName(NULL, szFile, MAX_PATH);
TCHAR tmp[64] ={0};
sprintf(tmp, "_%s_%d.dmp", __argv[1], GetTickCount());
_tcscat(szFile, tmp);
HANDLE hFile = ::CreateFile(szFile, FILE_ALL_ACCESS, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile)
{
MINIDUMP_EXCEPTION_INFORMATION mei;
ZeroMemory(&mei, sizeof(mei));
mei.ExceptionPointers = pExceptionInfo;
mei.ThreadId = GetCurrentThreadId();
mei.ClientPointers = TRUE;
if(pfnMiniDumpWriteDump) pfnMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &mei, NULL, NULL);
}
::CloseHandle(hFile);
}
}
ExitProcess(0);
return EXCEPTION_CONTINUE_SEARCH;
}
extern long __stdcall BRExceptionFilterWindows(_EXCEPTION_POINTERS* pExceptionInfo);
// Definitions
int BRWinMainCRTStartup()
{
SetUnhandledExceptionFilter(BRExceptionFilterWindows);
//DisableSetUnhandledExceptionFilter();
/*
#ifndef _UNICODE
return WinMainCRTStartup();
#else
return wWinMainCRTStartup();
#endif*/
return 0;
}
/////////////////////////////////////////////////////////////




