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
{
internal static Dictionary<uint, Measure> Measures = new Dictionary<uint, Measure>();
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));
}
}

View File

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