Marshalling p/invoke parameter bugs - Mono runtime source code?

Hi,

unfortunately I have lots of trouble using a certain assembly (let’s call it “c#link”) from a script. That assembly uses p/invoke to call an unmanaged dll (aka c++link). c#link uses to work without issues with .net (2.0 through 4.6).

All the problems are related to marshalling parameters into c++link or the other way around (callback from c++link to c#link).

At first, while marshalling char* from String as a simple parameter is correct, marshalling char* inside a struct fails. Also the callback function (inside the struct) is not correctly marshalled. It cannot be called from c++link. Had to pin these parameters for GC and marshall “manually”.

Then using StringBuilder as “container” for marshalling output char* parameters (c++link writes into a buffer provided by c#link) is erroneous after the first call: Although it contains the correct new data, .ToString() always results in the (cached) first value. So I needed to either recreate the StringBuilder every time or reset it (set length to zero).

Furthermore, when marshalling char* it fails to convert any string that contains umlauts (ANSI encoding). That’s the same when using StringBuilder as it is with Marshall.PtrToStringAnsi when using IntPtr. The string returned is null or empty. So I copy the data from IntPtr into a managed array and convert this array with Encoding.GetEncoding(1252).GetString.

But I still don’t know if these are all bugs :frowning:

Why is the runtime behaving so differently from .Net? Is it because of Mono 2.0? Will it ever be upgraded?
Should I report such bugs somewhere? Is there a list of such bugs and/or workarounds?
Where could I have a look at the used Mono runtime source in order to find the bugs more quickly?

Thanks

@ujr

These problems don’t sound like any fun to sort out. I hope that I can help a bit by answering some of your questions:

Why is the runtime behaving so differently from .Net? Is it because of Mono 2.0?

Yes, this is likely the case, the version of Mono Unity ships with currently has some bugs (as you’ve found) related to marshaling.

Will it ever be upgraded?

Yes, we’re working on this actively now. The best place to watch for new updates it the Experimental Scripting Previews section of the forums: http://forum.unity3d.com/forums/experimental-scripting-previews.107/

Should I report such bugs somewhere?

Yes, please report them! You can use the bug reported in the Unity editor. If possible, please include the Unity project that demonstrates the bug - that will really help us to correct them more quickly. Also, we need the source code for the native plugin, or at least the header files that define the interface to it, if possible.

Is there a list of such bugs and/or workarounds?

Unfortunately no, we don’t have a current list of bugs and work arounds. Ideally, we will correct any of the bugs that we can.

Where could I have a look at the used Mono runtime source in order to find the bugs more quickly?

The Mono source code shipped with Unity is available on Github here: https://github.com/Unity-Technologies/mono. Most of the marshaling code in Mono is located in this file, so it may be a place to start looking if you are interested: https://github.com/Unity-Technologies/mono/blob/unity-staging/mono/metadata/marshal.c

Hi Josh,

thank you for this extensive answer!

I need to prepare special demonstration files for the bug reports.

Thanks!