C异常处理

发布于 2024-11-14 19:03:54 字数 222 浏览 4 评论 0原文

我想知道如何处理 C 中的异常,特别是 EXCEPTION_GUARD_PAGE 异常。

我将尝试更简要地解释这种情况:

我想将一个部分/页面部分标记为 PAGE_GUARD ,每当程序遇到它们时,我想执行一些任务,我尝试用经典的 VirtualAlloc ->写->异常 命中,但我确实知道如何在 C 中捕获异常,

我将不胜感激。 提前致谢。

I would like to know how to handle exceptions in C, specifically EXCEPTION_GUARD_PAGE exceptions.

I will try explaining the situation more briefly:

I would like to mark a section/pages section as PAGE_GUARD, and whenever the program hits them I would like to perform a few tasks, I tried causing the exception with a classical VirtualAlloc -> Write -> Exception hit but I do know to catch the exception in C

I'd appreciate the help.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2024-11-21 19:03:55

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);

MSDN has everything you need:

In this particular case you want something in this vein:

__try
{
  /* Code that may cause exceptions... */
}
__except(GetExceptionCode() == EXCEPTION_GUARD_PAGE 
      ? EXCEPTION_EXECUTE_HANDLER
      : EXCEPTION_CONTINUE_SEARCH)
{
  /* Exception handling code... */
}

EDIT (in response to OP's comment):

The above code would do (MSDN is helpful again), but in this case there's more portable (no dependency on MSVC's extensions) and arguably less ugly solution: register a top-level exception filter using SetUnhandledExceptionFilter and let it handle the situation:

/* 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文