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

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

问题描述

大家好,我正在尝试在 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);

这是从这里找到的 WinRadio 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 将在不同的线程上执行,所以我选择 make is static.这样您就不会被诱惑访问 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,但请确保也pin 您传递的数据,以确保它不会被垃圾收集(有关详细信息,请参阅 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 以便 pin 它在内存中并防止它被垃圾收集.
        然而,这会为 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天全站免登陆