I'm implementing a C++ DLL which has to interoperate with C# code (and Unity framework), hence I need to be able to convert a std::string to System::String in C++. For this purpose, I wrote the following code:
app::String* native_string_to_managed_string(const std::string& native_string)
{
const auto encoding = (*app::Encoding__TypeInfo)->static_fields->utf8Encoding;
const auto managed_string = app::String_CreateStringFromEncoding((uint8_t*)&native_string.at(0),
static_cast<int32_t>(native_string.size()), encoding, nullptr);
return managed_string;
}
The called function is defined as:
String * String_CreateStringFromEncoding(uint8_t *bytes, int32_t byteLength, Encoding *encoding, MethodInfo *method)
(*app::Encoding__TypeInfo)->static_fields->utf16Encoding does not exist, either.
This seems to work fine for ASCII strings but as soon as I use e.g. Chinese symbols in the std::string, the conversion seems to be wrong since passing the managed string into a C# method afterwards causes a crash. I suppose the memory layout of strings is different among both languages so doing the conversion this way is problematic.
Is there any fool-proof way to convert a std::string to a System::String and vice versa with full UTF-8 support? My debugger showed the Chinese symbols correctly in the std::string variable, from the C++ layer there didn't seem to be an issue and obviously System::String supports UTF-8 as well.