C++/CLI:捕获所有(.NET/Win32/CRT)异常

hb.*_*hb. 7 .net exception-handling c++-cli

我知道这是不赞成的,但我在这里没有选择.我正在开发一个C++/CLI应用程序,它有一个我无法追踪的错误 - 主要是因为它绕过我当前的崩溃处理程序:

AppDomain::CurrentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler(&LogAndExit);
Application::ThreadException += gcnew ThreadExceptionEventHandler(&LogAndExit);
Application::SetUnhandledExceptionMode(UnhandledExceptionMode::CatchException);
try 
{ 
    Application::Run(gcnew frmMain()); 
} 
catch (Exception^ ex) 
{ 
    LogAndExit(ex); 
} 
catch (...) 
{ 
    LogAndExit(); 
}
Run Code Online (Sandbox Code Playgroud)

我想,标准.NET崩溃处理.MSDN报告称,某些CRT异常会破坏托管堆栈并以静默方式中止应用程序.

我一直在阅读_set_invalid_parameter_handler,但即使我收到LNK2001错误,它似乎也不能与/ clr:pure一起使用.我是对的,还是我只是PEBKACing它并丢失了一个lib文件?

Kir*_*ril 4

可以在/clr模式下运行吗?如果你可以尝试这个:

#include <exception>
Run Code Online (Sandbox Code Playgroud)

然后:

try
{
    try
    {
        Application::Run(gcnew frmMain()); 
    }
    catch(const exception& ex)
    {
        throw gcnew System::Exception(gcnew System::String(ex.what()));
    }
} 
catch (Exception^ ex) 
{ 
    LogAndExit(ex); 
} 
catch (...) 
{ 
    LogAndExit(); 
}
Run Code Online (Sandbox Code Playgroud)

另一件需要注意的事情是:如果您的应用程序是多线程的,那么您只会捕获正在运行的线程中的异常frmMain()。因此,在这种情况下,对整个应用程序进行包罗万象是不可能的!