From 4d4afd13450de7e5ecedc8429ba5f0f14ac95013 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Thu, 23 Jan 2014 11:59:33 +0200 Subject: [PATCH] InputText: Remove use of unsafe code --- Plugins/PluginInputText/Main.cs | 57 ++++++++++++------- .../PluginInputText/PluginInputText.csproj | 4 -- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Plugins/PluginInputText/Main.cs b/Plugins/PluginInputText/Main.cs index a3365eba..a666e30f 100644 --- a/Plugins/PluginInputText/Main.cs +++ b/Plugins/PluginInputText/Main.cs @@ -141,49 +141,66 @@ namespace InputText public static class Plugin { - internal static Dictionary Measures = new Dictionary(); + static IntPtr StringBuffer = IntPtr.Zero; [DllExport] - public unsafe static void Initialize(void** data, void* rm) + public static void Initialize(ref IntPtr data, IntPtr rm) { - uint id = (uint)((void*)*data); - Measures.Add(id, new Measure(new Rainmeter.API((IntPtr)rm))); + data = GCHandle.ToIntPtr(GCHandle.Alloc(new Measure(new Rainmeter.API(rm)))); } [DllExport] - public unsafe static void Finalize(void* data) + public static void Finalize(IntPtr data) { - uint id = (uint)data; - Measures[id].Dispose(); - Measures.Remove(id); + Measure measure = (Measure)GCHandle.FromIntPtr(data).Target; + measure.Dispose(); + GCHandle.FromIntPtr(data).Free(); + + if (StringBuffer != IntPtr.Zero) + { + Marshal.FreeHGlobal(StringBuffer); + StringBuffer = IntPtr.Zero; + } } [DllExport] - public unsafe static void Reload(void* data, void* rm, double* maxValue) + public static void Reload(IntPtr data, IntPtr rm, ref double maxValue) { - uint id = (uint)data; - Measures[id].Reload(new Rainmeter.API((IntPtr)rm), ref *maxValue); + Measure measure = (Measure)GCHandle.FromIntPtr(data).Target; + measure.Reload(new Rainmeter.API(rm), ref maxValue); } [DllExport] - public unsafe static double Update(void* data) + public static double Update(IntPtr data) { - uint id = (uint)data; - return Measures[id].Update(); + Measure measure = (Measure)GCHandle.FromIntPtr(data).Target; + return measure.Update(); } [DllExport] - public unsafe static char* GetString(void* data) + public static IntPtr GetString(IntPtr data) { - uint id = (uint)data; - fixed (char* s = Measures[id].GetString()) return s; + Measure measure = (Measure)GCHandle.FromIntPtr(data).Target; + if (StringBuffer != IntPtr.Zero) + { + Marshal.FreeHGlobal(StringBuffer); + StringBuffer = IntPtr.Zero; + } + + string stringValue = measure.GetString(); + if (stringValue != null) + { + StringBuffer = Marshal.StringToHGlobalUni(stringValue); + } + + return StringBuffer; } [DllExport] - public unsafe static void ExecuteBang(void* data, char* args) + public static void ExecuteBang(IntPtr data, IntPtr args) { - uint id = (uint)data; - Measures[id].ExecuteBang(new string(args)); + Measure measure = (Measure)GCHandle.FromIntPtr(data).Target; + measure.ExecuteBang(Marshal.PtrToStringUni(args)); } } diff --git a/Plugins/PluginInputText/PluginInputText.csproj b/Plugins/PluginInputText/PluginInputText.csproj index b013828f..1b074450 100644 --- a/Plugins/PluginInputText/PluginInputText.csproj +++ b/Plugins/PluginInputText/PluginInputText.csproj @@ -38,7 +38,6 @@ TRACE;DEBUG prompt 4 - true 1607 x86 @@ -48,14 +47,12 @@ TRACE prompt 4 - true 1607 x86 true TRACE;DEBUG;X64 - true full x64 prompt @@ -63,7 +60,6 @@ TRACE;X64 - true true none x64