calling C++ functions containing callbacks in C#

2019-02-23 19:16发布

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);

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?

many thanks

3条回答
倾城 Initia
2楼-- · 2019-02-23 19:57

A callback function is a code that is called by a dll (you're importing in this case) that performs some functions. You also need to learn how to work with delegates in c#. You can implement the code like this:

public void MyCallback(IntPtr p)
{
    //do something
}

and then your dll call will be like this:

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

If you need more guidance, post the C++ version of the code you want to convert and we can help you with the C# version.

查看更多
走好不送
3楼-- · 2019-02-23 20:10

All you need to do is to create a c# function that matches the signature of the delegate you declared. Create a delegate, hold on to a reference to this delegate so it doesn't get garbage collected, and call the dll import with the delegate as the callback.

so you would having something like this:

public void MyCallback(IntPtr P)
{
    //do something
}

// somewhere else in your code
var cb = new CallbackFunc(MyCallback);
CodecStart(..., cb, ...);
查看更多
爷的心禁止访问
4楼-- · 2019-02-23 20:11

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...
    }
}
  • 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.

  • 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).

EDIT:
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 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.
查看更多
登录 后发表回答