Method pointer marshaling with pointer argument in C #

I need to marshal a method pointer with a pointer argument, for example in C:

void (*callback)(int *x);

How will I write this as a structure field in C #?

Note: I do not mind that the CLR changes the pointer to me.

+3
source share
1 answer

If your method expects a callback to take a pointer to some structure, you can pass a managed callback when specifying P / Invoke DllImports as follows:

private delegate void MyCallback(IntPtr par);

[DllImport("MyLibrary.dll")]
public static extern void SomeFunction(MyCallback callback);

You can then marshal IntPtrinto the appropriate structure inside your actual callback method.

[change]

To pass a parameter intby reference, the following delegate signature should work best:

private delegate void MyCallback(ref int par);
+4
source

All Articles