如何将委托或函数指针从C#传递到C ++并使用InternalCall在那里调用它 [英] How to pass a delegate or function pointer from C# to C++ and call it there using InternalCall

查看:567
本文介绍了如何将委托或函数指针从C#传递到C ++并使用InternalCall在那里调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中具有以下设置:

I have the following setup in C#:

public delegate void CallbackDelegate(string message);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void setCallback(CallbackDelegate aCallback);

public void testCallbacks()
{
    System.Console.Write("Registering C# callback...\n");
    setCallback(callback01);
}

public void callback01(string message)
{
    System.Console.Write("callback 01 called: " + message + "\n");
}

这在C ++中(通过mono_add_internal_call正确注册了函数):

And this in C++ (the function is registered correctly via mono_add_internal_call ):

typedef void (*CallbackFunction)(const char*);
void setCallback(MonoDelegate* delegate)
{
    // How to convert the MonoDelegate to a proper function pointer?
    // So that I can call it like func("test");
}

调用C ++函数,并将 something 传递给委托变量. 但是现在呢?

The C++-function is called and something is passed to the delegate variable. But what now?

我环顾四周,发现函数"mono_delegate_to_ftnptr"提到了几次,从这些示例看来,这正是我所需要的.
但是,此功能似乎在我的mono(4.6)发行版中似乎根本不存在,因此我只能猜测它不再存在.

I looked around and found the function "mono_delegate_to_ftnptr" mentioned a few times, and from those examples it seems to be exactly what I need.
However, this function simply does not seem to exist in my distribution of mono (4.6), so I can only guess it does not exist any more.

我还找到了一些如何使用PInvoke进行操作的示例.这是我不想使用的东西-因为InternalCall可以更快地实现我的目的.
当然,如果PInvoke是唯一的方法,那就这样吧,但我对此表示怀疑.

I also found a few examples of how to do this with PInvoke. Which is something I do not want to use - since InternalCall is much faster for my purpose.
Of course, if PInvoke would be the only way, so be it, but I doubt that.

最后,我真的对如何从这里开始感到茫然.

In the end, I am really at a loss at how to proceed from here.

推荐答案

经过几个小时的挖掘,我终于找到了(the?)解决方案.
基本上,适用于PInvoke方法的方法在这里也适用,您可以从C#向C(++)传递函数指针而不是委托.
我希望可以直接传递委托的解决方案,但是您始终可以在C#中添加一些包装器代码,至少使其看起来像这样.

After some more hours of digging I finally found a (the?) solution.
Basically, what works for the PInvoke approach works here as well, you can pass a function pointer instead of a delegate from C# to C(++).
I'd prefer a solution where you can pass a delegate directly, but you can always add some wrapper code in C# to at least make it look like that.

解决方案:

C#:

public delegate void CallbackDelegate(string message);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void setCallback(IntPtr aCallback);

private CallbackDelegate del; 
public void testCallbacks()
{
    System.Console.Write("Registering C# callback...\n");
    del = new CallbackDelegate(callback01);
    setCallback(Marshal.GetFunctionPointerForDelegate(del));

    System.Console.Write("Calling passed C++ callback...\n");
}

public void callback01(string message)
{
    System.Console.Write("callback 01 called. Message: " + message + "\n");
}

C ++:

typedef void (*CallbackFunction)(MonoString*);
void setCallback(CallbackFunction delegate)
{
    std::cout << &delegate << std::endl;
    delegate(mono_string_new(mono_domain_get(), "Test string set in C++"));
}

但是请注意:您需要以某种方式在C#中保留委托(这就是为什么我将其分配给"del"的原因),否则它将被GC捕获,并且您的回调将变得无效.
当然,这是有道理的,但是在这种情况下,我觉得这很容易忘记.

Watch out, though: You need to keep the delegate around in C# somehow (which is why I assigned it to "del") or it will be caught by the GC and your callback will become invalid.
It makes sense, of course, but I feel this is easy to forget in this case.

这篇关于如何将委托或函数指针从C#传递到C ++并使用InternalCall在那里调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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