问题
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
回答1:
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
MyCallbackFuncwill be executed on a different thread, I chose to make isstatic. This way you won't be tempted to accessWinRadioWrapperdata members.For simplicity I passed an
IntPtr.Zeroparameter to the callback, but this can point to any data that you'd like to use in the callback.
[Please ignore this paragraph] Look intoMarshal.StructureToPtrif 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 (seeGCHandlefor 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.StructureToPtrshould 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 
GCHandlein order to pin it in memory and prevent it from being garbage-collected.
This, however, will add some maintenance overhead for theGCHandle. 
回答2:
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:
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, ...);
    来源:https://stackoverflow.com/questions/9659303/calling-c-functions-containing-callbacks-in-c-sharp