16,471
社区成员
发帖
与我相关
我的任务
分享
LONG WINAPI ApplicationCrashHandler(EXCEPTION_POINTERS* pException)
{
AfxMessageBox(_T("注意:系统发生异常,将会自动进行重启!"));
//重启系统
system("shutdown -r -t 0 -f");
return EXCEPTION_EXECUTE_HANDLER;
}
void DisableSetUnhandledExceptionFilter()
{
void *addr = (void*)GetProcAddress(LoadLibrary(_T("kernel32.dll")), "SetUnhandledExceptionFilter");
if (addr)
{
AfxMessageBox(_T("DisableSetUnhandledExceptionFilter"));
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);
}
}
BOOL CXXXApp::InitInstance()
{
//指定异常处理的回调函数
SetUnhandledExceptionFilter(ApplicationCrashHandler);
//禁用SetUnhandledExceptionFilter,防止异常回调函数被覆盖.
DisableSetUnhandledExceptionFilter();
}