简体   繁体   English

从非托管 C++ 代码调用 C# function 时无法处理异常

[英]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. 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.我想从代码中捕获 C#/C++ 异常,该异常位于 C++ function 调用之前

The idea of issue is following: I'm passing C# function pointer to C++ compiled library then from C++ invoke passed function. 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.我的 C++ 调用包含在try/catch中,我想从 C++/C# 函数中捕获异常。

So, I have .NET 5 application, which runs following code:所以,我有 .NET 5 应用程序,它运行以下代码:

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: libLinuxLoop.so 是 Linux 的 C++ 编译库(我使用的是 CentOS 7),它具有以下内容:

MyCppFunc.h: 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: 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.结果:由非托管代码调用的 C# function 引发异常,导致整个应用程序崩溃。 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. UPD:在 Windows 10 上捕获了异常,但我在 CentOS 7 上无法捕获它。

It works that way on Windows because the Windows stack frames support the exception mechanism through non-managed frames.它在 Windows 上以这种方式工作,因为 Windows 堆栈帧通过非托管帧支持异常机制。 It does not work that way on Linux because the Linux stack frames do not support the exception mechanism through non-managed frames.它在 Linux 上不起作用,因为 Linux 堆栈帧不支持通过非托管帧的异常机制。 – Eljay – 艾尔杰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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