Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit f19af3a

Browse files
authored
Merge pull request #58 from mahee96/master
x64 fix [ClikeStringArray.cs] + improved Exception Handling
2 parents 9e85373 + 2886f5e commit f19af3a

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Visual Studio Project Template C#/PluginInfrastructure/ClikeStringArray.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
22
using System;
33
using System.Collections.Generic;
4+
using System.Reflection;
45
using System.Runtime.InteropServices;
6+
using System.Windows.Forms;
57

68
namespace Kbg.NppPluginNET.PluginInfrastructure
79
{
@@ -18,10 +20,10 @@ public ClikeStringArray(int num, int stringCapacity)
1820
for (int i = 0; i < num; i++)
1921
{
2022
IntPtr item = Marshal.AllocHGlobal(stringCapacity);
21-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
23+
Marshal.WriteIntPtr(_nativeArray + (i * IntPtr.Size), item);
2224
_nativeItems.Add(item);
2325
}
24-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (num * IntPtr.Size)), IntPtr.Zero);
26+
Marshal.WriteIntPtr(_nativeArray + (num * IntPtr.Size), IntPtr.Zero);
2527
}
2628
public ClikeStringArray(List<string> lstStrings)
2729
{
@@ -30,10 +32,10 @@ public ClikeStringArray(List<string> lstStrings)
3032
for (int i = 0; i < lstStrings.Count; i++)
3133
{
3234
IntPtr item = Marshal.StringToHGlobalUni(lstStrings[i]);
33-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
35+
Marshal.WriteIntPtr(_nativeArray + (i * IntPtr.Size), item);
3436
_nativeItems.Add(item);
3537
}
36-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (lstStrings.Count * IntPtr.Size)), IntPtr.Zero);
38+
Marshal.WriteIntPtr(_nativeArray + (lstStrings.Count * IntPtr.Size), IntPtr.Zero);
3739
}
3840

3941
public IntPtr NativePointer { get { return _nativeArray; } }
@@ -52,12 +54,19 @@ List<string> _getManagedItems(bool unicode)
5254

5355
public void Dispose()
5456
{
55-
if (!_disposed)
57+
try
5658
{
57-
for (int i = 0; i < _nativeItems.Count; i++)
58-
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
59-
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
60-
_disposed = true;
59+
if (!_disposed)
60+
{
61+
for (int i = 0; i < _nativeItems.Count; i++)
62+
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
63+
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
64+
_disposed = true;
65+
}
66+
}
67+
catch (Exception e)
68+
{
69+
MessageBox.Show(MethodBase.GetCurrentMethod().ToString() +": "+ e.Message, this.GetType().Name);
6170
}
6271
}
6372
~ClikeStringArray()

0 commit comments

Comments
 (0)