简体   繁体   English

Mono C到正在运行的C#程序

[英]Mono C to a running C# program

I'm looking at communicating between a C++ program and a C# program using Mono. 我正在考虑使用Mono在C ++程序和C#程序之间进行通信。 On the Mono website they have some good examples of embedding Mono in a C program, starting the C# program, and then communicating between the two. 在Mono网站上,他们有一些很好的例子,可以将Mono嵌入到C程序中,启动C#程序,然后在两者之间进行通信。

mono_domain_assembly_open is used to open a C# assembly and mono_jit_exec is used to run it. mono_domain_assembly_open用于打开C#程序集,mono_jit_exec用于运行它。

My question is, is it possible to "connect" to an already running assembly? 我的问题是,是否可以“连接”到已经运行的程序集? The mono_jit_exec call would seem to me to be not needed since the C# assembly is already running. 由于C#程序集已在运行,因此我认为不需要mono_jit_exec调用。 Instead would there be an alternate call to "link" the C and C# programs? 相反,是否会有一个“链接”C和C#程序的替代调用?

Thanks, 谢谢,

mj MJ

I guess you mean a mono-specific way to exchange data/perform calls between the C side and the managed (C#) side in the same process (for different processes you will have to use a socket, like tcp/ip, and develop a protocol or use an existing one according to your needs). 我想你的意思是在同一个进程中,在C端和托管(C#)端之间交换数据/执行调用的单声道特定方式(对于不同的进程,你必须使用套接字,如tcp / ip,并开发一个协议或根据您的需要使用现有的协议)。

First, to call a C function from within managed code you have two main mechanisms: 首先,要从托管代码中调用C函数,您有两个主要机制:

  • a P/Invoke call (see http://www.mono-project.com/Interop_with_Native_Libraries for more details), using "__Internal" as the dll name to lookup the function in the executing program instead of an external library 一个P / Invoke调用(更多细节请参见http://www.mono-project.com/Interop_with_Native_Libraries ),使用“__Internal”作为dll名称来查找执行程序中的函数而不是外部库
  • an InternalCall call: this is a managed method implemented as a C function, see samples/embed/teste.c in the mono sources for a simple example 一个InternalCall调用:这是一个作为C函数实现的托管方法,有关简单示例,请参阅mono源中的samples / embed / teste.c

The main difference between the two is that with P/Invoke data marshalling happens (for example a managed string becomes a char* string on the C side), while with icalls you get direct access to managed objects. 两者之间的主要区别在于,使用P / Invoke进行数据编组(例如,托管字符串在C端变为char *字符串),而使用icall可以直接访问托管对象。

The main mechanism to go the other way (invoke a managed method from C) is to call mono_runtime_invoke (). 另一种方式(从C调用托管方法)的主要机制是调用mono_runtime_invoke()。 samples/embed/test-invoke.c has a few examples. samples / embed / test-invoke.c有几个例子。

Another way is to first marshal a delegate using P/Invoke: on the C side you will receive a function pointer which you can save (as long as you keep the managed delegate object alive, by storing it to a static field in a managed class, for example). 另一种方法是首先使用P / Invoke编组委托:在C端,您将收到一个可以保存的函数指针(只要您将托管委托对象保持活动状态,通过将其存储到托管类中的静态字段) , 例如)。 Later you can call using the function pointer on the C side and you will be transfered to the managed world automatically. 稍后您可以使用C侧的函数指针进行调用,您将自动转移到托管世界。

If the assembly you are executing with mono_jit_exec() is long running, you will need to perform the mono_runtime_invoke() on a separate thread that you will have started before calling mono_jit_exec(). 如果正在使用mono_jit_exec()执行的程序集长时间运行,则需要在调用mono_jit_exec()之前在已启动的单独线程上执行mono_runtime_invoke()。

When embedding mono it is customary to have a very short Main() method used only for initialization, so mono_jit_exec() returns quickly and you can control everything from within the main thread in the C side. 当嵌入单声道时,习惯上有一个非常短的Main()方法仅用于初始化,因此mono_jit_exec()快速返回,你可以控制C侧主线程内的所有内容。

If you start a separate thread, make sure you call mono_thread_attach() inside it before calling any other mono function or manipulating managed objects in it. 如果你启动一个单独的线程,请确保在调用任何其他单声道函数或操纵其中的托管对象之前调用其中的mono_thread_attach()。

You would need to use some sort of IPC (inter process communication) as your C program has no control over the C# program. 您需要使用某种IPC(进程间通信),因为您的C程序无法控制C#程序。 Possible ways would be named pipes, some sort of TCP/IP communication, Remoting (not sure of this applies on mono). 可能的方法是命名管道,某种TCP / IP通信,远程处理(不确定这适用于单声道)。

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

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