C# – How to pass a reference parameter (&) (not pointer) using p/invoke in C#

apic++interoppinvoke

I have a C++ API prototype

void Func(int& size);

How can I translate it to P/Invoke in C#?

From what I know, if I use

public static extern Func(ref int size); 

, the function will receive a pointer to the int instead of the value.

Best Solution

(Ooops... meant this as an answer, not a comment).

And when you call it from C++, the function will receive a pointer also. How else do you think references are implemented? Func() will treat that pointer as a reference, so how it gets there isn't important.