自 VC++2005 开始出于安全因素微软改变了 CRT 的行为,导致 CRT 不会通知被注册的 Unhandled Exception Filter。其原因在于CRT 通过调用 SetUnhandledExceptionFilter 并传递参数 NULL 来清除用户注册的 Unhandled Exception Filter。CRT调用如下:
SetUnhandledExceptionFilter(NULL);
UnhandledExceptionFilter(&ExceptionPointers);
因此为了避免出现这种情况,使用以下方式阻止CRT对SetUnhandledExceptionFilter的调用:
// 此函数一旦成功调用,之后对 SetUnhandledExceptionFilter 的调用将无效
void DisableSetUnhandledExceptionFilter()
{
HMODULE hDbgHelp = LoadLibrary(L"kernel32.dll");
void* addr = (void*)GetProcAddress(hDbgHelp,"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);
}
}
在注册 Unhandled Exception Filter 后调用 DisableSetUnhandledExceptionFilter() 函数,之后所有对 SetUnhandledExceptionFilter 的调用都将无效。CRT 对SetUnhandledExceptionFilter 的调用也将无效。
DisableSetUnhandledExceptionFilter调用:
SetUnhandledExceptionFilter(ExceptionFilter);//异常处理 生成dmp文件
DisableSetUnhandledExceptionFilter();//设置CRT对SetUnhandledExceptionFilter调用无效