Getting sensible info from CLR-to-SEH exceptions in a mixed mode C++ project

吃可爱长大的小学妹 提交于 2019-12-05 12:29:22

No, that's too late. All you got is the exception code. You might get something in ExceptionInformation if the original managed exception was caused by a processor fault. Like NullReference or AccessViolation. This won't be helpful since you don't know the original SEH exception anymore. Using COM give you a better mouse trap, the CLR implements IErrorInfo. But the managed code you're trying to run is probably not [ComVisible]. Calling the code through a managed stub that catches Exception might be a better angle.

lsalamon

There is similar answer to this question here:
Catching a CLR exception through unmanaged code

This was resolved in the following manner:

#import <mscorlib.tlb> raw_interfaces_only no_smart_pointers named_guids no_implementation

ATL::CComPtr< IErrorInfo > spErrorInfo;
ATL::CComPtr< mscorlib::_Exception > spCLRException;
ATL::CComPtr< mscorlib::_Exception > spCLRInnerException;

ATL::CComBSTR bstrCLRStackTrace;
ATL::CComBSTR bstrCLRMessage;

GetErrorInfo(0, &spErrorInfo)
spErrorInfo.QueryInterface(&spCLRException)
spCLRException->get_InnerException(&spCLRInnerException)
spCLRInnerException->get_StackTrace(&bstrCLRStackTrace)
spCLRInnerException->get_Message(&bstrCLRMessage)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!