Fix Win32 UTF8 marshaling

This commit is contained in:
Ethan Lee 2015-07-20 10:03:42 -04:00
parent f29dbb7e4d
commit 2e9752838d
2 changed files with 10 additions and 2 deletions

View file

@ -80,7 +80,7 @@ namespace SDL2
throw new ArgumentException("ManagedObj must be a string.", "ManagedObj"); throw new ArgumentException("ManagedObj must be a string.", "ManagedObj");
} }
var bytes = Encoding.UTF8.GetBytes(str); var bytes = Encoding.UTF8.GetBytes(str);
var mem = Marshal.AllocHGlobal(bytes.Length + 1); var mem = SDL.SDL_malloc((IntPtr) bytes.Length + 1);
Marshal.Copy(bytes, 0, mem, bytes.Length); Marshal.Copy(bytes, 0, mem, bytes.Length);
((byte*)mem)[bytes.Length] = 0; ((byte*)mem)[bytes.Length] = 0;
return mem; return mem;
@ -94,7 +94,7 @@ namespace SDL2
{ {
if (!_leaveAllocated) if (!_leaveAllocated)
{ {
Marshal.FreeHGlobal(pNativeData); SDL.SDL_free(pNativeData);
} }
} }

View file

@ -60,6 +60,14 @@ namespace SDL2
SDL_TRUE = 1 SDL_TRUE = 1
} }
/* malloc/free are used by the marshaler! -flibit */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr SDL_malloc(IntPtr size);
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void SDL_free(IntPtr memblock);
#endregion #endregion
#region SDL_rwops.h #region SDL_rwops.h