简体   繁体   中英

Can't handle exceptions when invoking C# function from unmanaged C++ code

The idea of issue is following: I'm passing C# function pointer to C++ compiled library then from C++ invoke passed function. I want to catch C#/C++ exceptions from code, which lays before C++ function invoke.

The idea of issue is following: I'm passing C# function pointer to C++ compiled library then from C++ invoke passed function. My C++ call is wrapped in try/catch and I want to catch exceptions from C++/C# functions.

So, I have .NET 5 application, which runs following code:

class NetProgram
{
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void CustomCppFunc(int id);

    [DllImport("/root/projects/LinuxLoop/bin/x64/Debug/libLinuxLoop.so")]
    protected static extern void strg_Create(IntPtr cb_is_my);

    static void Main(string[] args)
    {
        CustomCppFunc sharpDelegate = new CustomCppFunc(Smth.sharpStaticMethod);

        try
        {
            strg_Create(Marshal.GetFunctionPointerForDelegate(sharpDelegate));
        }
        catch (Exception e)
        {
            Console.WriteLine("Catched exception from C# Program: " + e.Message);
        }
        finally
        {
            Console.WriteLine("Finally from C# Program.");
        }
    }

    public class Smth
    {
        public static void sharpStaticMethod(IntPtr id)
        {
            Console.WriteLine("C#. sharpStaticMethod. Invoked.");

            Console.WriteLine("C#. sharpStaticMethod. Zero division.");
            var b = 0;
            var a = 1 / b;
        }
    }

}

libLinuxLoop.so is C++ compiled library for Linux (I'm using CentOS 7) and it has following content:

MyCppFunc.h:

#ifdef MYCPPFUNC
#define MYCPPFUNC __attribute__((dllexport))
#else
#define MYCPPFUNC __attribute__((dllimport))
#endif

typedef void(*CBIsMy)(int order_id);

extern "C" MYCPPFUNC void *strg_Create(CBIsMy cb_is_my);

MyCppFunc.cpp:

#include <cstdio>
#include <utility>
#include <limits.h>
#include "MyCppFunc.h"

void *strg_Create(CBIsMy cb_is_my) {
    std::fputs("C++. strg_Create. Invoked.\n", stdout);

    std::fputs("C++. strg_Create. Invoking C# delegate.\n", stdout);
    cb_is_my(1);

    return NULL;
}

Running application writes following messages:

C++. strg_Create. Invoked.
C++. strg_Create. Invoking C# delegate.
C#. sharpStaticMethod. Invoked.
C#. sharpStaticMethod. Zero division.
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.
at TestPlayground0806.NetProgram.Smth.sharpStaticMethod(Int32 id) in C:\Users\dev02\source\repos\TestPlayground0806\TestPlayground0806\NetProgram.cs:line 106
Aborted (core dumped)

Result: throwing an exception from C# function, called by unmanaged code, crashes the entire application. What am I supposed to in order to catch these kind of exceptions?

UPD: exception get caught on Windows 10 but I can't catch it on CentOS 7.

It works that way on Windows because the Windows stack frames support the exception mechanism through non-managed frames. It does not work that way on Linux because the Linux stack frames do not support the exception mechanism through non-managed frames. – Eljay

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM