C++ dll Pinvoke function with function pointer struct in parameter

Use IntPtr as the type for the function pointers. Then you can declare a delegate. Here is an example from code I wrote:

delegate void PrintLogDelegate(string msg);

PrintLogDelegate pld;
System.Runtime.InteropServices.GCHandle callbackPrintLog;
IntPtr ipCBPrintLog = IntPtr.Zero;

pld = new PrintLogDelegate(PrintLogFunc);
callbackPrintLog = System.Runtime.InteropServices.GCHandle.Alloc(pld);
ipCBPrintLog = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(pld);

void PrintLogFunc(string msg)


So ipCBPrintLog is what you pass to C++ as a callback.

Then in your Dispose you have to clean up: callbackPrintLog.Free();

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top