简体   繁体   English

从C ++调用(C#).NET Core方法

[英]Call (C#) .NET Core method from C++

I have 2 components: 我有2个组成部分:
- .Net Core Application running on Ubuntu OS. -在Ubuntu OS上运行的.Net Core应用程序。
- C++ shared library (.so) -C ++共享库(.so)

Now I want C++ component to be able to call .Net Core method, either passing interface to C++ component which will use this interface to callback method implementation or passing method as a parameter to C++ component. 现在,我希望C ++组件能够调用.Net Core方法,或者将接口传递给C ++组件,后者将使用该接口回调方法的实现,或者将方法作为参数传递给C ++组件。

High-level example what I am trying to achieve: 我试图实现的高级示例:
C# component : C#组件:

public interface IDevice
{
   void OnDataAvailable(string data);
}

public class Device: IDevice
{
   [DllImport("sampleCPPLibrary.so")]
   private static extern int SetReceiver(IDevice receiver);  
   public void OnDataAvailable(string data)
   {
      Console.WriteLine(data);
   }
   public void Initialize()
   {
      SetReceiver(IDevice(this))
   }
}

C++ component: C ++组件:

extern "C" {
void SetReceiver(IReceiver * receiver)
{
    receiver->OnDataAvailable(10);
}
}

Basically, what I am trying to do is just to pass some kind of "callback" to C++ component and call this "callback" when some event occurs in C++ component. 基本上,我想做的只是将某种“回调”传递给C ++组件,并在C ++组件中发生某些事件时调用此“回调”。

See See this issue from comments I constructed code where C# calls C and gives it callback delegate. 参阅“从注释中看到此问题” ,我在C#调用C的地方构造了代码,并为其提供了回调委托。 Thus from C then it calls C#, and passes additional int type argument. 因此,它随后从C调用C#,并传递其他int类型参数。 See comments here from endurox project , and attached c-callback.tar.gz for working example. 请参阅endurox项目的评论 ,并附上c-callback.tar.gz作为工作示例。

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

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