Throw a SEH exception with a message

I have a C ++ library that is being called from a C # application using the C ++ / CLI cover library. In C ++ code, I want to throw an exception if something goes wrong. It is translated into SEHException in a managed world. However, the message containing the original exception naturally disappeared.

How to pass this message to C #?

  • Can I somehow configure the translation so that, for example, it is MyCppExceptionconverted to MyManagedExceptionby marching the message?
  • Is there any way to throw an exception so that the SEHException contains the message?

I want to avoid catching my exception in all functions of the C ++ / CLI shell and, if possible, rebuilding.

// C++ cpp.h
class MyException { const char* what() const { return "OH HI THERE"; } };
MyException e;
void throw_function()
{
    throw e;
}

// Wrapper
#include "cpp.h"
public ref class A
{
public:
    static Throw() { throw_function(); }
}

public ref class E: public Exception
{
    System::String^ GetMessage();
}    

// App.cs
try
{
    A.Throw()
}
catch (E e)    
{
    Console.WriteLine(e.GetMessage()); // I would like to do something like this...
}
catch (SEHException seh)
{
    Console.WriteLine(seh.Message); // Or this...
}
+4
source share

Source: https://habr.com/ru/post/1584296/


All Articles