Tuesday, March 1, 2011

How can I pass a reference parameter (&) (not pointer) using p/invoke in C#?

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.

From stackoverflow
  • (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.

0 comments:

Post a Comment