公司项目中有一个项目用到的是Unicode工程,在添加dump文件生成的时候原来的不能用了,因此鄙人在原来的基础上进行了一部分的修改,使其能够在Unicode工程下发挥作用
#ifndef __BRExceptionFilter_H__
#define __BRExceptionFilter_H__
// Includes
#include "imagehlp.h"
typedef BOOL (__stdcall *FNDUMPWRITEDUMP) (HANDLE hProcess, DWORD ProcessId, HANDLE hFile, MINIDUMP_TYPE DumpType, const PMINIDUMP_EXCEPTION_INFORMATION pcExceptionParam, LPCVOID pcUserStreamParam, LPCVOID pcCallbackParam);
/*PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
PMINIDUMP_CALLBACK_INFORMATION CallbackParam);*/
// Declarations
extern long __stdcall BRExceptionFilterWindows(_EXCEPTION_POINTERS* pExceptionInfo);
extern long BRExceptionFilter(_EXCEPTION_POINTERS* pExceptionInfo);
// Predefineds
//#pragma comment(linker, "/subsystem:windows /entry:BRWinMainCRTStartup")
// Declarations
extern "C" int BRWinMainCRTStartup(void);
#endif//__BRExceptionFilter_H__
/
下面是cpp部分
/
//
// File: BRExceptionFilter.cpp
// Author: Blood's Rust
// Date: 2012.03.20
// Version: 1.0.0.0
//
/
#include "stdafx.h"
#include "BRExceptionFilter.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)
{
/*MessageBox(NULL, _T("The unhandled exception filter for windows."), _T("Exception"), MB_OK);*/
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};
wsprintf(tmp, _T("_%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;
}
/