Reputation:
I have a very basic call from C# to C++. The function call takes three parameters:
extern "C" int CLIENT_API example(const char, char * const, const unsigned int);
The C# code imports that function as:
[DllImport("my.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int example(in sbyte id, byte[] buffer, in uint bufferSize);
When I call the function from C# like this:
uint bufferSize = 400;
byte[] buffer = new byte[bufferSize];
example(0, buffer, bufferSize);
I log on the C++ size variant values for both id and bufferSize (every run has different values for those two variables). What am I doing wrong? I have other interop functions that pass pointers and strings to my C++ DLL and they aren't having any issues. It seems like it just happens with the more primitive types of byte and uint.
Upvotes: 1
Views: 225
Reputation: 4937
The in
modifier makes the argument a read-only ref
argument. It's not the same as the [In]
attribute, which is a marshalling hint.
You're passing 0
and bufferSize
, and C# is taking their addresses (the literal 0
by way of a temporary variable) and passing those.
You're getting varying values because the address is different each time.
Remove the in
modifiers so that C# passes those values rather than addresses.
Upvotes: 2