How to stop C++ app from crashing when an Access Violation exception happens in a C# function?

80 Views Asked by At

I am building a C++ application that uses Mono to load in C# code and execute it from within C++. I have a MonoMethod* to a C# function that can have code that may cause an Access Violation exception to happen, which obviously makes the application crash.

However, what I would need to happen is instead to somehow catch this exception and pause the code execution, handle the AV, and then continue the code safely.

Is there any way to achieve this? Preferably without libraries, and a solution that will either work on both Windows and Linux (I target both OSs), or a solution for Windows and Linux separately.

I tried using the try-except statement from Microsoft (running on Windows, obviously), and even literally copy and pasting the example code didn't work. The application crashed in both Debug and Release.

I also tried the Signal Handler from this post, but none of them worked, no matter what configuration I used.

I tried running both on Windows, so maybe the second one would work on Linux, but I haven't tested that as I want to find a solution for Windows first.

0

There are 0 best solutions below