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
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:
and then your dll call will be like this:
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.
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:
Here's a simple implementation that will put it all together.
Note that because
MyCallbackFunc
will be executed on a different thread, I chose to make isstatic
. This way you won't be tempted to accessWinRadioWrapper
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 (seeGCHandle
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.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
.