在 Mac 上从C++代码调用时"coreclr_create_delegate"不起作用

`coreclr_create_delegate` doesn't work when called from C++ code on Mac

本文关键字:coreclr delegate 不起作用 create 上从 Mac C++ 代码 调用      更新时间:2023-10-16

我需要从 Mac 上的C++代码调用 C# 库中声明的方法。我正在使用.Net Core框架2.0.x(2.0.3,2.0.4(。

为了使它工作,我调用coreclr_initialize它成功运行并返回0。然后,我通过传递所需的参数来调用coreclr_create_delegate,如下所示:

auto no_del = []( auto x ) { (void)(x); };
auto csharp_runIt = std::unique_ptr<csharp_runIt_t, decltype(no_del)>(nullptr, no_del);
int status = coreclr_create_delegate (
                                          hostHandle,
                                          domainId,
                                          assemblyName.c_str(),
                                          entryPointType.c_str(),
                                          entryPointName.c_str(),
                                          reinterpret_cast<void**>(&csharp_runIt)
                                          );

在这里,hostHandledomainId是从coreclr_initialize收到的。其余值为:assemblyName(dll 名称(、entryPointType(类名(和entryPointName(函数名(。运行它会返回带有十六进制代码的负值:0x80070002

我还传递库名、类名和方法名作为参数。由于没有多少人尝试过,因此在线上没有太多帮助。

如果该类不在全局命名空间中,则需要指定该类的完全限定名称。例如,您需要在此处将entryPointType设置为samplelibrary.SampleClass

好吧,

所以我明白了什么问题。这。NetCore 库位于命名空间下,因此未创建coreclr_create_delegate。因此,要么不使用命名空间,要么在调用委托时也使用它C++命名空间。

这。Netcore代码应该是这样的:

//namespace samplelibrary //This was causing error
//{
    public class SampleClass
    {
        public static string MyFunc()
        {
            return "arrived";
        }
    }
//}