我想知道如何处理C中的exception,特别是EXCEPTION_GUARD_PAGE
exception。
我会试着更简单地解释一下情况:
我想标记一个章节/网页部分PAGE_GUARD
,并且每当程序击中他们,我想执行一些任务,我试图导致与经典的VirtualAlloc -> Write -> Exception
exceptionVirtualAlloc -> Write -> Exception
打,但我知道赶上C中的例外
我会很感激的帮助。 提前致谢。
MSDN有你需要的一切:
在这个特定的情况下,你需要这样的一些东西:
__try { /* Code that may cause exceptions... */ } __except(GetExceptionCode() == EXCEPTION_GUARD_PAGE ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { /* Exception handling code... */ }
编辑(回应OP的评论):
上面的代码会做( MSDN是有用的),但在这种情况下,有更多的可移植性(不依赖于MSVC的扩展),可以说是不太丑陋的解决方案:使用SetUnhandledExceptionFilter注册顶级异常过滤器,让它处理的情况:
/* Our top-level exception filter. */ LONG WINAPI MyUnhandledExceptionFilter(EXCEPTION_POINTERS* exceptionInfo) { DWORD exceptionCode = exceptionInfo->ExceptionRecord->ExceptionCode; if(exceptionCode == EXCEPTION_GUARD_PAGE) { /* Handle the situation somehow... */ /* ...and resume the exception from the place the exception was thrown. */ return EXCEPTION_CONTINUE_EXECUTION; } else { /* Unknown exception, let another exception filter worry about it. */ return EXCEPTION_CONTINUE_SEARCH; } } /* Somewhere else in the code: */ SetUnhandledExceptionFilter(&MyUnhandledExceptionFilter);