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
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?
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?