调用包含在C#中的回调C ++函数 [英] calling C++ functions containing callbacks in C#

查看:147
本文介绍了调用包含在C#中的回调C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有IM试图让我的头围绕在调用这个C ++在C#中的功能:

hey all im trying to get my head around calling this c++ function in c#:

BOOL __stdcall CodecStart(int hRadio,void __stdcall (*CallbackFunc)(void *),void *CallbackTarget);

这是从这里找到了万瑞API http://www.winradio.com/home/ g305_sdk.htm

this is from a WinRadio api found here http://www.winradio.com/home/g305_sdk.htm.

我发现其他人问起呼吁在网上这个特定的功能,他们有:

i did find that other people asked about calling this specific function on the net and they had:

    public delegate void CallbackFunc( IntPtr p);

    [DllImport("WRG305API.dll")]
    public static extern bool CodecStart(int hRadio, CallbackFunc func, IntPtr CallbackTarget);

但我无法弄清楚如何实施这一更进一步。

but i cant figure out how to implement this further.

任何想法或指导,如何调用呢?

any thoughts or guidance as to how to call this?

非常感谢

推荐答案

下面是一个简单的实现,将会把它放在一起。

Here's a simple implementation that will put it all together.

class WinRadioWrapper
{
    public delegate void CallbackFunc( IntPtr pData );

    [DllImport( "WRG305API.dll" )]
    public static extern bool CodecStart( int hRadio, CallbackFunc func, IntPtr CallbackTarget );

    public bool CodecStartTest(int hRadio)
    {
        bool bStarted = CodecStart( hRadio, MyCallbackFunc, IntPtr.Zero );
        return bStarted;
    }

    // Note: this method will be called from a different thread!
    static void MyCallbackFunc( IntPtr pData )
    {
        // Sophisticated work goes here...
    }
}


  • 请注意,由于 MyCallbackFunc 将在不同的线程来执行,我选择了把为静态 。这样,你就不会被诱惑访问 WinRadioWrapper 数据成员。

    • Note that because MyCallbackFunc will be executed on a different thread, I chose to make is static. This way you won't be tempted to access WinRadioWrapper data members.

      有关简单起见,我通过了 IntPtr.Zero 参数回调,但可以指向您想在回调可以使用任何数据。搜索结果的 [请忽略本段] 。看看 Marshal.StructureToPtr 如果你想传递数据给回调,但一定要还固定您传递,以确保它不是垃圾收集的数据(见的GCHandle 更多详细信息)。

      For simplicity I passed an IntPtr.Zero parameter to the callback, but this can point to any data that you'd like to use in the callback.

      [Please ignore this paragraph] Look into Marshal.StructureToPtr if you'd like to pass data to the callback, but make sure to also pin the data that you're passing in order to make sure it's not garbage-collected (see GCHandle for more details).

      编辑:
      结果由有趣的词 svick (感谢!),我意识到我是混合复制的对象带有固定在某一。
      结果因此,要理清头绪:


      With the interesting words by svick (thanks!), I realize I was mixing a copied object with a pinned one.
      So, to sort things out:


      • Marshal.StructureToPtr 如果你想复制一个现有的数据结构,然后将它传递给回调函数应该被使用。

      • 如果,在另一方面,你想使用和现有的数据结构(例如,用于修改其内容)时,你应该使用的GCHandle 的它在内存中,prevent它被垃圾收集的。照片然而,这会增加一些维护开销的GCHandle

      • Marshal.StructureToPtr should be used if you want to copy an existing data structure and then pass it to the callback function.
      • If, on the other hand, you'd like to use and existing data structure (e.g. for modifying its content), the you should use GCHandle in order to pin it in memory and prevent it from being garbage-collected.
        This, however, will add some maintenance overhead for the GCHandle.

      这篇关于调用包含在C#中的回调C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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