Browse Source

Fixed wide string marshaling on UTF-32 platforms.

pull/547/head
triton 11 years ago
parent
commit
144d26749f
  1. 10
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 13
      src/Runtime/Helpers.cs

10
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -189,7 +189,15 @@ namespace CppSharp.Generators.CSharp
if (Equals(encoding, Encoding.Unicode) || if (Equals(encoding, Encoding.Unicode) ||
Equals(encoding, Encoding.BigEndianUnicode)) Equals(encoding, Encoding.BigEndianUnicode))
return string.Format("Marshal.PtrToStringUni({0})", varName); {
if (Context.Driver.TargetInfo.WCharWidth == 16)
return string.Format("Marshal.PtrToStringUni({0})", varName);
const string encodingName = "System.Text.Encoding.UTF32";
return string.Format(
"CppSharp.Runtime.Helpers.MarshalEncodedString({0}, {1})",
varName, encodingName);
}
throw new NotSupportedException(string.Format("{0} is not supported yet.", throw new NotSupportedException(string.Format("{0} is not supported yet.",
Context.Driver.Options.Encoding.EncodingName)); Context.Driver.Options.Encoding.EncodingName));

13
src/Runtime/Helpers.cs

@ -1,11 +1,24 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
namespace CppSharp.Runtime namespace CppSharp.Runtime
{ {
public static class Helpers public static class Helpers
{ {
public static string MarshalEncodedString(IntPtr ptr, Encoding encoding)
{
var size = 0;
while (Marshal.ReadInt32(ptr, size) != 0)
size += sizeof(int);
var buffer = new byte[size];
Marshal.Copy(ptr, buffer, 0, buffer.Length);
return encoding.GetString(buffer);
}
#if WINDOWS #if WINDOWS
[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl)] [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl)]
#else #else

Loading…
Cancel
Save