passing (function) pointers between c and mono
Hi after refering to http://www.mono-project.com/Embedding_Mono i can call methods from managed code by using mono_runtime_invoke.
Now i want to call a method in the managed code with a function pointer (or at least some pointer) as argument from native c code
managed code
public delegate void MyDelegate ();
//method i want to call from native code
public static MyDelegate mono_method(MyDelegate c_ptr)
{
//...do sth
return c_ptr;
}
native code
typedef void (*FUNC_PTR)();
FUNC_PTR my_fct_ptr = some_c_function;
//calling the managed method
MonoObject *result_of_mono_method =
mono_runtime_invoke(mono_method, NULL, my_fct_ptr, NULL);
edit: to point out the problem
how can i call开发者_JAVA技巧public static unsafe int* mono_method(int *c_ptr)
from native c code, without using dllImport.
You have several options. One is to add an internal call that takes a IntPtr (the function pointer) and the arguments: you will then cast the pointer to the function pointer type and call it normally from C code. Using something like libffi can help to overcome the limitation of having just one function pointer type, it depends how many you need, you didn't specify.
Another option is to use Reflection.Emit to build a dynamic method: in it you will use the calli IL instruction to invoke the function pointer directly.
I'm not exactly sure what you're trying to ask here, but this is the easiest way to do a call back.
精彩评论