InputText: Remove use of unsafe code

This commit is contained in:
Birunthan Mohanathas 2014-01-23 11:59:33 +02:00
parent 8078f49106
commit 4d4afd1345
2 changed files with 37 additions and 24 deletions

View File

@ -141,49 +141,66 @@ namespace InputText
public static class Plugin public static class Plugin
{ {
internal static Dictionary<uint, Measure> Measures = new Dictionary<uint, Measure>(); static IntPtr StringBuffer = IntPtr.Zero;
[DllExport] [DllExport]
public unsafe static void Initialize(void** data, void* rm) public static void Initialize(ref IntPtr data, IntPtr rm)
{ {
uint id = (uint)((void*)*data); data = GCHandle.ToIntPtr(GCHandle.Alloc(new Measure(new Rainmeter.API(rm))));
Measures.Add(id, new Measure(new Rainmeter.API((IntPtr)rm)));
} }
[DllExport] [DllExport]
public unsafe static void Finalize(void* data) public static void Finalize(IntPtr data)
{ {
uint id = (uint)data; Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
Measures[id].Dispose(); measure.Dispose();
Measures.Remove(id); GCHandle.FromIntPtr(data).Free();
if (StringBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(StringBuffer);
StringBuffer = IntPtr.Zero;
}
} }
[DllExport] [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; Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
Measures[id].Reload(new Rainmeter.API((IntPtr)rm), ref *maxValue); measure.Reload(new Rainmeter.API(rm), ref maxValue);
} }
[DllExport] [DllExport]
public unsafe static double Update(void* data) public static double Update(IntPtr data)
{ {
uint id = (uint)data; Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
return Measures[id].Update(); return measure.Update();
} }
[DllExport] [DllExport]
public unsafe static char* GetString(void* data) public static IntPtr GetString(IntPtr data)
{ {
uint id = (uint)data; Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
fixed (char* s = Measures[id].GetString()) return s; if (StringBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(StringBuffer);
StringBuffer = IntPtr.Zero;
}
string stringValue = measure.GetString();
if (stringValue != null)
{
StringBuffer = Marshal.StringToHGlobalUni(stringValue);
}
return StringBuffer;
} }
[DllExport] [DllExport]
public unsafe static void ExecuteBang(void* data, char* args) public static void ExecuteBang(IntPtr data, IntPtr args)
{ {
uint id = (uint)data; Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
Measures[id].ExecuteBang(new string(args)); measure.ExecuteBang(Marshal.PtrToStringUni(args));
} }
} }

View File

@ -38,7 +38,6 @@
<DefineConstants>TRACE;DEBUG</DefineConstants> <DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>1607</NoWarn> <NoWarn>1607</NoWarn>
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x86</PlatformTarget>
</PropertyGroup> </PropertyGroup>
@ -48,14 +47,12 @@
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>1607</NoWarn> <NoWarn>1607</NoWarn>
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x86</PlatformTarget>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DefineConstants>TRACE;DEBUG;X64</DefineConstants> <DefineConstants>TRACE;DEBUG;X64</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
@ -63,7 +60,6 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<DefineConstants>TRACE;X64</DefineConstants> <DefineConstants>TRACE;X64</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<DebugType>none</DebugType> <DebugType>none</DebugType>
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>