c# - CLI/C++ 和 C# : How to capture C++ exceptions in Release Mode

标签 c# c++ exception

我有这个问题:

Managed C++ 中,我使用了一个 C# DLL,我在其中注册了一个在 中定义的 delegate托管 C++C# 库执行此 delegate 并在其中引发 native exception。在Debug 模式下,try/catch 会被执行,但在Release 模式下,它不会。但是在同样的场景下,如果异常是由 100% 托管代码抛出的,我可以捕获它。

这是我正在使用的代码:

托管 C++:

class CNativeClass
{
    public:
      int one;
}

// This C method is registred like Delegate in C#
void OnMsgReceived(ManagedObjectInCSharp^ obj)
{
    CNativeClass* pelota;
    pelota->one = 0; // <-- This procude a NullPointerException
    Console::WriteLine(L"OnMsgReceived");
}

我的 C# 使用 try/catch 执行此 delegate。在 Debug 中没问题,但在 Release 中就不行了。

//But if I defined the method like this:
void OnMsgReceived(ManagedObjectInCSharp^ obj)
{
    try  
    {
        CNativeClass* pelota;
        pelota->one = 0; // <-- This procude a NullPointerException
        Console::WriteLine(L"OnMsgReceived");
    }
    catch(const char* str)
    {
    }
}

我的 C# 在 Release模式下捕获异常。

最佳答案

好吧,也许这不太可能,但请尝试将此属性添加到您的 C# 方法中:

[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]

http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

关于c# - CLI/C++ 和 C# : How to capture C++ exceptions in Release Mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12814724/

相关文章:

c# - 设计 ASP :ListBox as UL

c# - 模型失效时未捕获 Oxyplot 的鼠标事件

c++ - Google mock - 至少是多种期望中的一种

c# - 在这种情况下,一般异常处理不是那么糟糕吗?

Windows DataGridView BindingSource 索引超出范围异常

python django 异常处理

c# - 如何下载没有url的pdf

c# - 如何将附加属性绑定(bind)到 ComboBox 的 DisplayMemberPath?

C++ 回文程序总是给出 0 (false) 作为输出问题;我的代码哪里错了?

c++ - 没有调用单例的构造函数