STRINGREF pString = NULL;
LPCUTF8 sz = (LPCUTF8)MAYBE_UNALIGNED_READ(pNativeValue, _PTR);
- if (!sz)
- {
- pString = NULL;
- }
- else
+ if (sz)
{
MethodDescCallSite convertToManaged(METHOD__CUTF8MARSHALER__CONVERT_TO_MANAGED);
ARG_SLOT args[] =
{
- PtrToArgSlot(pNativeValue),
+ PtrToArgSlot(sz),
};
pString = convertToManaged.Call_RetSTRINGREF(args);
}
[DllImport("UTF8TestNative", CallingConvention = CallingConvention.Cdecl)]
public static extern void TestStructWithUtf8Field(Utf8Struct utfStruct);
+ [DllImport("UTF8TestNative", CallingConvention = CallingConvention.Cdecl)]
+ public static extern void SetStringInStruct(ref Utf8Struct utfStruct, [MarshalAs(UnmanagedType.LPUTF8Str)] string str);
+
public static void TestUTF8StructMarshalling(string[] utf8Strings)
{
Utf8Struct utf8Struct = new Utf8Struct();
}
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
CompareWithUTF8Encoding();
+
+ string testString = "StructTestString\uD83D\uDE00";
+
+ SetStringInStruct(ref utf8Struct, testString);
+
+ if (utf8Struct.FirstName != testString)
+ {
+ throw new Exception("Incorrect UTF8 string marshalled back from native to managed.");
+ }
}
unsafe static void CompareWithUTF8Encoding()
free_utf8_string(pszNative);
}
+extern "C" DLL_EXPORT void __cdecl SetStringInStruct(FieldWithUtf8* fieldStruct, char* str)
+{
+ size_t strLength = strlen(str);
+ char* strCopy = (char*)CoreClrAlloc(sizeof(char) * strlen(str) + 1);
+
+ memcpy(strCopy, str, strLength + 1);
+ fieldStruct->pFirst = strCopy;
+ fieldStruct->index = 0;
+}
+
// test c# out keyword
extern "C" DLL_EXPORT void __cdecl StringParameterRefOut(/*out*/ char **s, int index)
{