From cbbc74bc76d8e482e423281a00be139c545c54d0 Mon Sep 17 00:00:00 2001 From: Jameson Ernst Date: Tue, 25 Jun 2013 10:07:07 -0700 Subject: [PATCH 1/6] Added .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa8a8a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +*.userprefs From 1741030481e56e7f6be233692e9c5cc1517e39d2 Mon Sep 17 00:00:00 2001 From: Jameson Ernst Date: Tue, 25 Jun 2013 21:36:38 -0700 Subject: [PATCH 2/6] Added UTF-8 string marshaling --- SDL2#.csproj | 1 + src/LPUtf8StrMarshaler.cs | 44 +++++++++++++++++++++++++ src/SDL2.cs | 68 +++++++++++++++++++-------------------- 3 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 src/LPUtf8StrMarshaler.cs diff --git a/SDL2#.csproj b/SDL2#.csproj index 7d5a0a5..15e4e4c 100644 --- a/SDL2#.csproj +++ b/SDL2#.csproj @@ -155,6 +155,7 @@ + diff --git a/src/LPUtf8StrMarshaler.cs b/src/LPUtf8StrMarshaler.cs new file mode 100644 index 0000000..3de5288 --- /dev/null +++ b/src/LPUtf8StrMarshaler.cs @@ -0,0 +1,44 @@ +using System; +using System.Text; +using System.Runtime.InteropServices; + +namespace SDL2 { + internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler { + static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler(); + + static ICustomMarshaler GetInstance (string cookie) { + return _instance; + } + + public object MarshalNativeToManaged (IntPtr pNativeData) { + var ptr = (byte*)pNativeData; + while (*ptr != 0) + ptr++; + var bytes = new byte[ptr - (byte*)pNativeData]; + Marshal.Copy(pNativeData, bytes, 0, bytes.Length); + return Encoding.UTF8.GetString(bytes); + } + + public IntPtr MarshalManagedToNative (object ManagedObj) { + var str = ManagedObj as string; + if (str == null) + throw new ArgumentException("ManagedObj must be a string.", "ManagedObj"); + var bytes = Encoding.UTF8.GetBytes(str); + var mem = Marshal.AllocHGlobal(bytes.Length + 1); + Marshal.Copy(bytes, 0, mem, bytes.Length); + ((byte*)mem)[bytes.Length] = 0; + return mem; + } + + public void CleanUpNativeData (IntPtr pNativeData) { + Marshal.FreeHGlobal(pNativeData); + } + + public void CleanUpManagedData (object ManagedObj) { + } + + public int GetNativeDataSize () { + return -1; + } + } +} diff --git a/src/SDL2.cs b/src/SDL2.cs index 4ffd1a7..c62676f 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -70,9 +70,9 @@ namespace SDL2 [DllImport(nativeLibName, EntryPoint = "SDL_RWFromFile", CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr INTERNAL_SDL_RWFromFile( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string file, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string mode ); @@ -160,7 +160,7 @@ namespace SDL2 [DllImport(nativeLibName, EntryPoint = "SDL_GetHint", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr INTERNAL_SDL_GetHint( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name ); public static string SDL_GetHint(string name) @@ -172,17 +172,17 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_SetHint( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string value ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_SetHintWithPriority( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string value, SDL_HintPriority priority ); @@ -203,7 +203,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_SetError( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -267,7 +267,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_Log( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -275,7 +275,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_LogVerbose( int category, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -283,7 +283,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_LogDebug( int category, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -291,7 +291,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_LogInfo( int category, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -299,7 +299,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_LogWarn( int category, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -307,7 +307,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_LogError( int category, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -315,7 +315,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_LogCritical( int category, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -324,7 +324,7 @@ namespace SDL2 public static extern void SDL_LogMessage( int category, SDL_LogPriority priority, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -333,7 +333,7 @@ namespace SDL2 public static extern void SDL_LogMessageV( int category, SDL_LogPriority priority, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string fmt, __arglist ); @@ -546,7 +546,7 @@ namespace SDL2 /* IntPtr refers to an SDL_Window* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr SDL_CreateWindow( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string title, int x, int y, @@ -653,7 +653,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr SDL_GetWindowData( IntPtr window, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name ); @@ -752,13 +752,13 @@ namespace SDL2 /* IntPtr refers to a function pointer */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr SDL_GL_GetProcAddress( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string proc ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_GL_ExtensionSupported( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string extension ); @@ -829,7 +829,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr SDL_SetWindowData( IntPtr window, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name, IntPtr userdata ); @@ -898,7 +898,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_SetWindowTitle( IntPtr window, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string title ); @@ -920,7 +920,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_VideoInit( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string driver_name ); @@ -2192,7 +2192,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_SetClipboardText( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text ); @@ -3370,7 +3370,7 @@ namespace SDL2 /* Get a scancode from a human-readable name */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_Scancode SDL_GetScancodeFromName( - [In()] [MarshalAs(UnmanagedType.LPStr)] string name + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name ); /* Wrapper for SDL_GetKeyName */ @@ -3387,7 +3387,7 @@ namespace SDL2 /* Get a key code from a human-readable name */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_Keycode SDL_GetKeyFromName( - [In()] [MarshalAs(UnmanagedType.LPStr)] string name + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name ); /* Start accepting Unicode text input events, show keyboard */ @@ -3661,14 +3661,14 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_JoystickGetGUIDString( SDL_JoystickGUID guid, - [MarshalAs(UnmanagedType.LPStr)] + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] System.Text.StringBuilder pszGUID, int cbGUID ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_JoystickGUID SDL_JoystickGetGUIDFromString( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string pchGUID ); @@ -3750,7 +3750,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GameControllerAddMapping( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string mappingString ); @@ -3831,7 +3831,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_GameControllerAxis SDL_GameControllerGetAxisFromString( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string pchString ); @@ -3863,7 +3863,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_GameControllerButton SDL_GameControllerGetButtonFromString( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string pchString ); @@ -4335,7 +4335,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_AudioInit( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string driver_name ); @@ -4466,7 +4466,7 @@ namespace SDL2 /* uint refers to an SDL_AudioDeviceID */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern uint SDL_OpenAudioDevice( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string device, int iscapture, ref SDL_AudioSpec desired, From 1c6c04f2e2c6ac2b04ebf6f36cfc35e121b96c3b Mon Sep 17 00:00:00 2001 From: Jameson Ernst Date: Tue, 25 Jun 2013 22:12:29 -0700 Subject: [PATCH 3/6] Apply UTF-8 marshaling to return values and removed extra wrapping layer --- src/SDL2.cs | 285 ++++++++++++++++------------------------------------ 1 file changed, 84 insertions(+), 201 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index c62676f..2e35107 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -158,18 +158,13 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_ClearHints(); - [DllImport(nativeLibName, EntryPoint = "SDL_GetHint", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetHint( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetHint( [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name ); - public static string SDL_GetHint(string name) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetHint(name) - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_SetHint( [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] @@ -194,13 +189,10 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_ClearError(); - [DllImport(nativeLibName, EntryPoint = "SDL_GetError", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetError(); - public static string SDL_GetError() - { - return Marshal.PtrToStringAnsi(INTERNAL_SDL_GetError()); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetError(); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_SetError( [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] @@ -417,15 +409,10 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] private static extern void SDL_GetVersion(ref SDL_version ver); - [DllImport(nativeLibName, EntryPoint = "SDL_GetRevision", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetRevision(); - public static string SDL_GetRevision() - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetRevision() - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetRevision(); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GetRevisionNumber(); @@ -593,15 +580,10 @@ namespace SDL2 ref SDL_DisplayMode mode ); - [DllImport(nativeLibName, EntryPoint = "SDL_GetCurrentVideoDriver", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetCurrentVideoDriver(); - public static string SDL_GetCurrentVideoDriver() - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetCurrentVideoDriver() - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetCurrentVideoDriver(); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GetDesktopDisplayMode( int displayIndex, @@ -632,17 +614,12 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GetNumVideoDrivers(); - [DllImport(nativeLibName, EntryPoint = "SDL_GetVideoDriver", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetVideoDriver( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetVideoDriver( int index ); - public static string SDL_GetVideoDriver(int index) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetVideoDriver(index) - ); - } - + /* window refers to an SDL_Window* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern float SDL_GetWindowBrightness( @@ -722,17 +699,12 @@ namespace SDL2 public static extern IntPtr SDL_GetWindowSurface(IntPtr window); /* window refers to an SDL_Window* */ - [DllImport(nativeLibName, EntryPoint = "SDL_GetWindowTitle", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetWindowTitle( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetWindowTitle( IntPtr window ); - public static string SDL_GetWindowTitle(IntPtr window) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetWindowTitle(window) - ); - } - + /* texture refers to an SDL_Texture* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GL_BindTexture( @@ -1725,17 +1697,12 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_FreePalette(IntPtr palette); - [DllImport(nativeLibName, EntryPoint = "SDL_GetPixelFormatName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetPixelFormatName( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetPixelFormatName( uint format ); - public static string SDL_GetPixelFormatName(uint format) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetPixelFormatName(format) - ); - } - + /* format refers to an SDL_PixelFormat* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_GetRGB( @@ -2181,15 +2148,10 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_HasClipboardText(); - [DllImport(nativeLibName, EntryPoint = "SDL_GetClipboardText", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetClipboardText(); - public static string SDL_GetClipboardText(string name) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetClipboardText() - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetClipboardText(); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_SetClipboardText( [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] @@ -3357,15 +3319,9 @@ namespace SDL2 public static extern void SDL_GetScancodeFromKey(SDL_Keycode key); /* Wrapper for SDL_GetScancodeName */ - [DllImport(nativeLibName, EntryPoint="SDL_GetScancodeName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetScancodeName(SDL_Scancode scancode); - /* Get a human-readable name for a scancode */ - public static string SDL_GetScancodeName(SDL_Scancode scancode) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetScancodeName(scancode) - ); - } + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetScancodeName(SDL_Scancode scancode); /* Get a scancode from a human-readable name */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -3374,15 +3330,9 @@ namespace SDL2 ); /* Wrapper for SDL_GetKeyName */ - [DllImport(nativeLibName, EntryPoint="SDL_GetKeyName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetKeyName(SDL_Keycode key); - /* Get a human-readable name for a key */ - public static string SDL_GetKeyName(SDL_Keycode key) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetKeyName(key) - ); - } + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetKeyName(SDL_Keycode key); /* Get a key code from a human-readable name */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -3593,28 +3543,18 @@ namespace SDL2 public static extern int SDL_JoystickIndex(IntPtr joystick); /* joystick refers to an SDL_Joystick* */ - [DllImport(nativeLibName, EntryPoint = "SDL_JoystickName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_JoystickName( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string INTERNAL_SDL_JoystickName( IntPtr joystick ); - public static string SDL_JoystickName(IntPtr joystick) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_JoystickName(joystick) - ); - } - - [DllImport(nativeLibName, EntryPoint = "SDL_JoystickNameForIndex", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_JoystickNameForIndex( + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_JoystickNameForIndex( int device_index ); - public static string SDL_JoystickNameForIndex(int device_index) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_JoystickNameForIndex(device_index) - ); - } - + /* joystick refers to an SDL_Joystick* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_JoystickNumAxes(IntPtr joystick); @@ -3755,60 +3695,37 @@ namespace SDL2 ); [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerMappingForGUID", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GameControllerMappingForGUID( + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GameControllerMappingForGUID( SDL_JoystickGUID guid ); - public static string SDL_GameControllerMappingForGUID( - SDL_JoystickGUID guid - ) { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GameControllerMappingForGUID(guid) - ); - } - /* gamecontroller refers to an SDL_GameController* */ - [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerMapping", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GameControllerMapping( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GameControllerMapping( IntPtr gamecontroller ); - public static string SDL_GameControllerMapping( - IntPtr gamecontroller - ) { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GameControllerMapping(gamecontroller) - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_IsGameController(int joystick_index); - [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerNameForIndex", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GameControllerNameForIndex( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string INTERNAL_SDL_GameControllerNameForIndex( int joystick_index ); - public static string SDL_GameControllerNameForIndex(int joystick_index) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GameControllerNameForIndex(joystick_index) - ); - } - + /* IntPtr refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr SDL_GameControllerOpen(int joystick_index); /* gamecontroller refers to an SDL_GameController* */ - [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GameControllerName( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GameControllerName( IntPtr gamecontroller ); - public static string SDL_GameControllerName(IntPtr gamecontroller) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GameControllerName(gamecontroller) - ); - } - + /* gamecontroller refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_GameControllerGetAttached( @@ -3835,18 +3752,12 @@ namespace SDL2 string pchString ); - [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerGetStringForAxis", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GameControllerGetStringForAxis( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GameControllerGetStringForAxis( SDL_GameControllerAxis axis ); - public static string SDL_GameControllerGetStringForAxis( - SDL_GameControllerAxis axis - ) { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GameControllerGetStringForAxis(axis) - ); - } - + /* gamecontroller refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis( @@ -3867,18 +3778,12 @@ namespace SDL2 string pchString ); - [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerGetStringForButton", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GameControllerGetStringForButton( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GameControllerGetStringForButton( SDL_GameControllerButton button ); - public static string SDL_GameControllerGetStringForButton( - SDL_GameControllerButton button - ) { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GameControllerGetStringForButton(button) - ); - } - + /* gamecontroller refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton( @@ -4094,15 +3999,10 @@ namespace SDL2 public static extern int SDL_HapticIndex(IntPtr haptic); /* haptic refers to an SDL_Haptic* */ - [DllImport(nativeLibName, EntryPoint = "SDL_HapticName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_HapticName(int device_index); - public static string SDL_HapticName(int device_index) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_HapticName(device_index) - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_HapticName(int device_index); + /* haptic refers to an SDL_Haptic* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_HapticNewEffect( @@ -4353,47 +4253,30 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_FreeWAV(IntPtr audio_buf); - [DllImport(nativeLibName, EntryPoint = "SDL_GetAudioDeviceName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetAudioDeviceName( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetAudioDeviceName( int index, int iscapture ); - public static string SDL_GetAudioDeviceName( - int index, - int iscapture - ) { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetAudioDeviceName(index, iscapture) - ); - } - + /* dev refers to an SDL_AudioDeviceID */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_AudioStatus SDL_GetAudioDeviceStatus( uint dev ); - [DllImport(nativeLibName, EntryPoint = "SDL_GetAudioDriver", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetAudioDriver(int index); - public static string SDL_GetAudioDriver(int index) - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetAudioDriver(index) - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetAudioDriver(int index); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_AudioStatus SDL_GetAudioStatus(); - [DllImport(nativeLibName, EntryPoint = "SDL_GetCurrentAudioDriver", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetCurrentAudioDriver(); - public static string SDL_GetCurrentAudioDriver() - { - return Marshal.PtrToStringAnsi( - INTERNAL_SDL_GetCurrentAudioDriver() - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string SDL_GetCurrentAudioDriver(); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GetNumAudioDevices(int iscapture); From 4868a53683a39bb9a14dae23415f120f040bb556 Mon Sep 17 00:00:00 2001 From: Jameson Ernst Date: Tue, 25 Jun 2013 23:16:43 -0700 Subject: [PATCH 4/6] Added UTF-8 support to supplementary libs Style changes for consistency --- src/LPUtf8StrMarshaler.cs | 56 ++++++++++++++++++++++++++++++++------- src/SDL2_image.cs | 4 +-- src/SDL2_mixer.cs | 43 +++++++++++------------------- src/SDL2_ttf.cs | 50 ++++++++++++++-------------------- 4 files changed, 84 insertions(+), 69 deletions(-) diff --git a/src/LPUtf8StrMarshaler.cs b/src/LPUtf8StrMarshaler.cs index 3de5288..8d2b801 100644 --- a/src/LPUtf8StrMarshaler.cs +++ b/src/LPUtf8StrMarshaler.cs @@ -1,28 +1,63 @@ +/* SDL2# - C# Wrapper for SDL2 + * + * Copyright (c) 2013 Ethan Lee. + * + * This software is provided 'as-is', without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in a + * product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source distribution. + * + * Ethan "flibitijibibo" Lee + * + */ + using System; using System.Text; using System.Runtime.InteropServices; -namespace SDL2 { - internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler { - static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler(); +namespace SDL2 +{ + internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler + { + private static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler(); - static ICustomMarshaler GetInstance (string cookie) { + private static ICustomMarshaler GetInstance(string cookie) + { return _instance; } - public object MarshalNativeToManaged (IntPtr pNativeData) { + public object MarshalNativeToManaged(IntPtr pNativeData) + { var ptr = (byte*)pNativeData; while (*ptr != 0) + { ptr++; + } var bytes = new byte[ptr - (byte*)pNativeData]; Marshal.Copy(pNativeData, bytes, 0, bytes.Length); return Encoding.UTF8.GetString(bytes); } - public IntPtr MarshalManagedToNative (object ManagedObj) { + public IntPtr MarshalManagedToNative(object ManagedObj) + { var str = ManagedObj as string; if (str == null) + { throw new ArgumentException("ManagedObj must be a string.", "ManagedObj"); + } var bytes = Encoding.UTF8.GetBytes(str); var mem = Marshal.AllocHGlobal(bytes.Length + 1); Marshal.Copy(bytes, 0, mem, bytes.Length); @@ -30,14 +65,17 @@ namespace SDL2 { return mem; } - public void CleanUpNativeData (IntPtr pNativeData) { + public void CleanUpNativeData(IntPtr pNativeData) + { Marshal.FreeHGlobal(pNativeData); } - public void CleanUpManagedData (object ManagedObj) { + public void CleanUpManagedData(object ManagedObj) + { } - public int GetNativeDataSize () { + public int GetNativeDataSize() + { return -1; } } diff --git a/src/SDL2_image.cs b/src/SDL2_image.cs index 66dbe88..6337211 100644 --- a/src/SDL2_image.cs +++ b/src/SDL2_image.cs @@ -90,7 +90,7 @@ namespace SDL2 /* IntPtr refers to an SDL_Surface* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr IMG_Load( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string file ); @@ -98,7 +98,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr IMG_LoadTexture( IntPtr renderer, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string file ); diff --git a/src/SDL2_mixer.cs b/src/SDL2_mixer.cs index 88212be..a97a034 100644 --- a/src/SDL2_mixer.cs +++ b/src/SDL2_mixer.cs @@ -199,7 +199,7 @@ namespace SDL2 /* IntPtr refers to a Mix_Music* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr Mix_LoadMUS( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string file ); @@ -222,27 +222,17 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int Mix_GetNumChunkDecoders(); - [DllImport(nativeLibName, EntryPoint = "Mix_GetChunkDecoder", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_Mix_GetChunkDecoder(int index); - public static string Mix_GetChunkDecoder(int index) - { - return Marshal.PtrToStringAnsi( - INTERNAL_Mix_GetChunkDecoder(index) - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string Mix_GetChunkDecoder(int index); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int Mix_GetNumMusicDecoders(); - [DllImport(nativeLibName, EntryPoint = "Mix_GetMusicDecoder", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_Mix_GetMusicDecoder(int index); - public static string Mix_GetMusicDecoder(int index) - { - return Marshal.PtrToStringAnsi( - INTERNAL_Mix_GetMusicDecoder(index) - ); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string Mix_GetMusicDecoder(int index); + /* music refers to a Mix_Music* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern Mix_MusicType Mix_GetMusicType(IntPtr music); @@ -462,7 +452,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int Mix_SetMusicCMD( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string command ); @@ -474,17 +464,14 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int Mix_SetSoundFonts( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string paths ); - [DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_Mix_GetSoundFonts(); - public static string Mix_GetSoundFonts() - { - return Marshal.PtrToStringAnsi(INTERNAL_Mix_GetSoundFonts()); - } - + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string Mix_GetSoundFonts(); + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int Mix_EachSoundFont( SoundFontDelegate function, diff --git a/src/SDL2_ttf.cs b/src/SDL2_ttf.cs index a86d621..4c346c1 100644 --- a/src/SDL2_ttf.cs +++ b/src/SDL2_ttf.cs @@ -95,7 +95,7 @@ namespace SDL2 /* IntPtr refers to a TTF_Font* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_OpenFont( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string file, int ptsize ); @@ -103,7 +103,7 @@ namespace SDL2 /* IntPtr refers to a TTF_Font* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_OpenFontIndex( - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string file, int ptsize, long index @@ -166,29 +166,19 @@ namespace SDL2 public static extern int TTF_FontFaceIsFixedWidth(IntPtr font); /* font refers to a TTF_Font* */ - [DllImport(nativeLibName, EntryPoint = "TTF_FontFaceFamilyName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_FontFaceFamilyName( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string TTF_FontFaceFamilyName( IntPtr font ); - public static string TTF_FontFaceFamily(IntPtr font) - { - return Marshal.PtrToStringAnsi( - INTERNAL_TTF_FontFaceFamilyName(font) - ); - } - + /* font refers to a TTF_Font* */ - [DllImport(nativeLibName, EntryPoint = "TTF_FontFaceStyleName", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_FontFaceStyleName( + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + public static extern string TTF_FontFaceStyleName( IntPtr font ); - public static string TTF_FontFaceStyleName(IntPtr font) - { - return Marshal.PtrToStringAnsi( - INTERNAL_TTF_FontFaceStyleName(font) - ); - } - + /* font refers to a TTF_Font* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int TTF_GlyphIsProvided(IntPtr font, ushort ch); @@ -209,7 +199,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int TTF_SizeText( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, ref int w, ref int h @@ -219,7 +209,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int TTF_SizeUTF8( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, ref int w, ref int h @@ -238,7 +228,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderText_Solid( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg ); @@ -247,7 +237,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderUTF8_Solid( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg ); @@ -272,7 +262,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderText_Shaded( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg, SDL.SDL_Color bg @@ -282,7 +272,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderUTF8_Shaded( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg, SDL.SDL_Color bg @@ -310,7 +300,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderText_Blended( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg ); @@ -319,7 +309,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderUTF8_Blended( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg ); @@ -336,7 +326,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderText_Blended_Wrapped( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg, uint wrapped @@ -346,7 +336,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr TTF_RenderUTF8_Blended_Wrapped( IntPtr font, - [In()] [MarshalAs(UnmanagedType.LPStr)] + [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg, uint wrapped From 00f353693e457cf524e89954d5440d07718d805d Mon Sep 17 00:00:00 2001 From: Jameson Ernst Date: Tue, 25 Jun 2013 23:51:05 -0700 Subject: [PATCH 5/6] Fixed leftover internal names --- src/SDL2.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index 2e35107..7255a91 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -3545,7 +3545,7 @@ namespace SDL2 /* joystick refers to an SDL_Joystick* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] - public static extern string INTERNAL_SDL_JoystickName( + public static extern string SDL_JoystickName( IntPtr joystick ); @@ -3711,7 +3711,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] - public static extern string INTERNAL_SDL_GameControllerNameForIndex( + public static extern string SDL_GameControllerNameForIndex( int joystick_index ); From 19d46023215e7211081d8d6e5e55086e57a64a5d Mon Sep 17 00:00:00 2001 From: Jameson Ernst Date: Wed, 26 Jun 2013 11:02:44 -0700 Subject: [PATCH 6/6] Don't free strings returned from SDL Accept nulls for marshaling --- src/LPUtf8StrMarshaler.cs | 40 ++++++++++++++++++++++++++++------- src/SDL2.cs | 44 +++++++++++++++++++-------------------- src/SDL2_mixer.cs | 6 +++--- src/SDL2_ttf.cs | 4 ++-- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/LPUtf8StrMarshaler.cs b/src/LPUtf8StrMarshaler.cs index 8d2b801..3371ed0 100644 --- a/src/LPUtf8StrMarshaler.cs +++ b/src/LPUtf8StrMarshaler.cs @@ -32,15 +32,34 @@ namespace SDL2 { internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler { - private static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler(); + public const string LeaveAllocated = "LeaveAllocated"; + + private static ICustomMarshaler + _leaveAllocatedInstance = new LPUtf8StrMarshaler(true), + _defaultInstance = new LPUtf8StrMarshaler(false); private static ICustomMarshaler GetInstance(string cookie) { - return _instance; + switch (cookie) + { + case "LeaveAllocated": + return _leaveAllocatedInstance; + default: + return _defaultInstance; + } + } + + private bool _leaveAllocated; + + public LPUtf8StrMarshaler(bool leaveAllocated) + { + _leaveAllocated = leaveAllocated; } public object MarshalNativeToManaged(IntPtr pNativeData) { + if (pNativeData == IntPtr.Zero) + return null; var ptr = (byte*)pNativeData; while (*ptr != 0) { @@ -53,6 +72,8 @@ namespace SDL2 public IntPtr MarshalManagedToNative(object ManagedObj) { + if (ManagedObj == null) + return IntPtr.Zero; var str = ManagedObj as string; if (str == null) { @@ -65,16 +86,19 @@ namespace SDL2 return mem; } - public void CleanUpNativeData(IntPtr pNativeData) - { - Marshal.FreeHGlobal(pNativeData); - } - public void CleanUpManagedData(object ManagedObj) { } - public int GetNativeDataSize() + public void CleanUpNativeData(IntPtr pNativeData) + { + if (!_leaveAllocated) + { + Marshal.FreeHGlobal(pNativeData); + } + } + + public int GetNativeDataSize () { return -1; } diff --git a/src/SDL2.cs b/src/SDL2.cs index 7255a91..8d879d1 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -159,7 +159,7 @@ namespace SDL2 public static extern void SDL_ClearHints(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetHint( [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string name @@ -190,7 +190,7 @@ namespace SDL2 public static extern void SDL_ClearError(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetError(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -410,7 +410,7 @@ namespace SDL2 private static extern void SDL_GetVersion(ref SDL_version ver); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetRevision(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -581,7 +581,7 @@ namespace SDL2 ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetCurrentVideoDriver(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -615,7 +615,7 @@ namespace SDL2 public static extern int SDL_GetNumVideoDrivers(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetVideoDriver( int index ); @@ -700,7 +700,7 @@ namespace SDL2 /* window refers to an SDL_Window* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetWindowTitle( IntPtr window ); @@ -1698,7 +1698,7 @@ namespace SDL2 public static extern void SDL_FreePalette(IntPtr palette); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetPixelFormatName( uint format ); @@ -2149,7 +2149,7 @@ namespace SDL2 public static extern SDL_bool SDL_HasClipboardText(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetClipboardText(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -3320,7 +3320,7 @@ namespace SDL2 /* Wrapper for SDL_GetScancodeName */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetScancodeName(SDL_Scancode scancode); /* Get a scancode from a human-readable name */ @@ -3331,7 +3331,7 @@ namespace SDL2 /* Wrapper for SDL_GetKeyName */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetKeyName(SDL_Keycode key); /* Get a key code from a human-readable name */ @@ -3544,13 +3544,13 @@ namespace SDL2 /* joystick refers to an SDL_Joystick* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_JoystickName( IntPtr joystick ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_JoystickNameForIndex( int device_index ); @@ -3695,13 +3695,13 @@ namespace SDL2 ); [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerMappingForGUID", CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GameControllerMappingForGUID( SDL_JoystickGUID guid ); /* gamecontroller refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GameControllerMapping( IntPtr gamecontroller ); @@ -3710,7 +3710,7 @@ namespace SDL2 public static extern SDL_bool SDL_IsGameController(int joystick_index); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GameControllerNameForIndex( int joystick_index ); @@ -3721,7 +3721,7 @@ namespace SDL2 /* gamecontroller refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GameControllerName( IntPtr gamecontroller ); @@ -3753,7 +3753,7 @@ namespace SDL2 ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GameControllerGetStringForAxis( SDL_GameControllerAxis axis ); @@ -3779,7 +3779,7 @@ namespace SDL2 ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GameControllerGetStringForButton( SDL_GameControllerButton button ); @@ -4000,7 +4000,7 @@ namespace SDL2 /* haptic refers to an SDL_Haptic* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_HapticName(int device_index); /* haptic refers to an SDL_Haptic* */ @@ -4254,7 +4254,7 @@ namespace SDL2 public static extern void SDL_FreeWAV(IntPtr audio_buf); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetAudioDeviceName( int index, int iscapture @@ -4267,14 +4267,14 @@ namespace SDL2 ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetAudioDriver(int index); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_AudioStatus SDL_GetAudioStatus(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string SDL_GetCurrentAudioDriver(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] diff --git a/src/SDL2_mixer.cs b/src/SDL2_mixer.cs index a97a034..8d669df 100644 --- a/src/SDL2_mixer.cs +++ b/src/SDL2_mixer.cs @@ -223,14 +223,14 @@ namespace SDL2 public static extern int Mix_GetNumChunkDecoders(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string Mix_GetChunkDecoder(int index); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int Mix_GetNumMusicDecoders(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string Mix_GetMusicDecoder(int index); /* music refers to a Mix_Music* */ @@ -469,7 +469,7 @@ namespace SDL2 ); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string Mix_GetSoundFonts(); [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] diff --git a/src/SDL2_ttf.cs b/src/SDL2_ttf.cs index 4c346c1..4edea5c 100644 --- a/src/SDL2_ttf.cs +++ b/src/SDL2_ttf.cs @@ -167,14 +167,14 @@ namespace SDL2 /* font refers to a TTF_Font* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string TTF_FontFaceFamilyName( IntPtr font ); /* font refers to a TTF_Font* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] + [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)] public static extern string TTF_FontFaceStyleName( IntPtr font );