Search code examples
c#monopinvoke

What encoding does Mono use when marshalling strings in P/Invoke?


I'm reading Mono's marshalling guide, for the purpose of marshalling strings between Objective-C++ and Unity (for iOS games), and I'm somewhat confused about string marshalling encodings in Mono.

I understand that string encodings for marshalling are specified by the CharSet enum, but it allows only Ansi and Unicode (=UTF-16) (and Auto, but that just makes the runtime choose between the first two).

But the guide also keeps saying that Mono marshals strings in UTF-8. How is that possible? Is this magic that happens if no CharSet is specified? Does this mean that I can safely pass -[NSString UTF8String] from Objective-C++ to C#, if I don't specify a CharSet? And does this imply an incompatibility with Microsoft's runtime?


Solution

  • The Ansi charset value is meaningless on non-Windows operating systems, so in that case utf-8 is used, like is your case with iOS.

    This is an incompatibility with the MS runtime, but the much bigger incompatibility in this case is that the MS runtime doesn't run at all on iOS:).