Hi,
Siegfried Heintze said:
I'm calling C functions (in a DLL) using platform invoke. This is NOT a COM
dll.
Is it possible to pass a C# function pointer to C so it can call C#
functions?
Yes, create a delegate type for that callback function and use the delegate
inside the function prototype for the callback parameter.
It's also important when you create a delegate instance and pass it to the
unmanaged side that you keep a managed reference to that instance alive.
Don't pass a temporarly delegate instance.
e.g.
public delegate void MyCallBackHandler(int a, int c);
[DllImport(....)] // the delegate marshals to a ftn-ptr.
public extern static SetCallback( MyCallBackHandler callback );
class SomeClass
{
MyCallBackHandler callback;
public void SomeMethod()
{
callback = new MyCallBackHandler( OnCallBack );
SetCallback( callback );
}
private void OnCallBack( int a, int c )
{
//...
}
}
HTH,
greetings