Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling C++ functions containing callbacks in 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);

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

like image 449
Gelion Avatar asked Mar 11 '12 22:03

Gelion


1 Answers

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.
like image 199
AVIDeveloper Avatar answered Oct 02 '22 02:10

AVIDeveloper