如何调用c#(mono,.net)方法,从本地c [英] howto call c# (mono , .net) methods, delegates from native c

查看:197
本文介绍了如何调用c#(mono,.net)方法,从本地c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从原生c代码(以及如何操作)调用以托管代码(可能在类或库中)编写的c#方法?

is it possible to call c# methods written in managed code (maybe in a class or a library) from a native c code (and how)?

thx

编辑:使用c#我大多指的是单声道或甚至portable.net和操作系统是Linux

edit: with "c#" i mostly refer to mono or even portable.net and the OS is Linux

推荐答案

您的C代码可以定义函数来注册回调。 C#代码可以P /调用这些函数,并将被管理的代理作为参数传递。编组代码将透明地将这些转换为C函数指针。

Your C code can define functions to register callbacks. The C# code can P/Invoke these functions, and pass managed delegates as arguments. The marshalling code will transparently transform these to C function pointers.

或者,从C侧接近它,您可以使用Mono嵌入API加载程序集,查找MonoMethods并调用它们。

Alternatively, approaching it from the C side, you can use the Mono embedding API to load assemblies, look up MonoMethods, and invoke them.

使用嵌入API要复杂得多。如果您的入口点在C中,您必须使用嵌入API,但是可能更容易简单地写一个托管方法来执行回调注册和任何其他托管设置,那么您只需要加载和调用该单个方法从C代码。

Using the embedding API is much more complicated. If your entry point is in C, you'll have to use the embedding API, but it's likely easier to simply write a managed method to do the callback registration and any other managed setup, then you only have to load and invoke that single method from C code.

这篇关于如何调用c#(mono,.net)方法,从本地c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆