混合本机/托管可执行文件中的最终托管异常处理程序?
我有一个使用 /clr 编译的 MFC 应用程序,并且我正在尝试为未捕获的托管异常实现最终处理程序。 对于本机异常,重写 CWinApp::ProcessWndProcException 是可行的。
Jeff 的 CodeProject 文章 中建议的两个事件,Application.ThreadException< /code> 和
AppDomain.CurrentDomain.UnhandledException
不会引发。
任何人都可以建议一种为混合可执行文件提供最终托管异常处理程序的方法吗?
更新:
看来这些异常处理程序仅在 Application.Run
或类似的下游触发(有工作线程风格,不记得名称。)如果您想真正全局捕获托管异常您确实需要安装 SEH 过滤器。 您不会遇到 System.Exception,如果您想要调用堆栈,则必须滚动自己的 Walker。
在关于此主题的 MSDN 论坛问题中,建议在 try ... catch (Exception^)
中覆盖主 MFC 线程的足够低级别的点。 例如,CWinApp::Run
。 这可能是一个很好的解决方案,但我还没有考虑任何性能或稳定性影响。 在退出之前,您将有机会使用调用堆栈进行日志记录,并且可以避免默认的 Windows 未处理的异常行为。
I have an MFC application compiled with /clr and I'm trying to implement a final handler for otherwise un-caught managed exceptions. For native exceptions, overriding CWinApp::ProcessWndProcException
works.
The two events suggested in Jeff's CodeProject article,Application.ThreadException
and AppDomain.CurrentDomain.UnhandledException
, are not raised.
Can anyone suggest a way to provide a final managed exception handler for a mixed executable?
Update:
It appears that these exception handlers are only triggered downstream of Application.Run
or similar (there's a worker thread flavor, can't remember the name.) If you want to truly globally catch a managed exception you do need to install an SEH filter. You're not going to get a System.Exception
and if you want a callstack you're going to have to roll your own walker.
In an MSDN forum question on this topic it was suggested to override a sufficiently low-level point of the main MFC thread in a try ... catch (Exception^)
. For instance, CWinApp::Run
. This may be a good solution but I haven't looked at any perf or stability implications. You'll get a chance to log with a call stack before you bail and you can avoid the default windows unahndled exception behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要?”
使用以下内容不会引发事件:
Why "should?"
The events are not raised using the below:
使用这两个异常处理程序应该可以工作。 您确定已将它们添加到将被调用并正确设置的位置(即,在应用程序的托管入口点中 - 您确实添加了一个,对吗?)
Using those two exception handlers should work. Are you sure you've added them in a place where they're going to be called and properly set (ie, in your application's managed entry point -- you did put one in, right?)
浏览一下互联网,您会发现您需要安装一个过滤器,以使非托管异常通过过滤器到达您的 AppDomain。 来自 CLR和未处理的异常过滤器:
Taking a look around the internets, you'll find that you need to install a filter to get the unmanaged exceptions passing the filters on their way to your AppDomain. From CLR and Unhandled Exception Filters: