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

Commit 0732ea8

Browse files
committed
Extracted ClikeStringArray
1 parent 594c028 commit 0732ea8

File tree

5 files changed

+73
-61
lines changed

5 files changed

+73
-61
lines changed

Demo Plugin/NppManagedPluginDemo/NppManagedPluginDemo.VS2015.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
<Compile Include="..\..\Visual Studio Project Template C#\DllExport\DllExportAttribute.cs">
5050
<Link>DllExport\DllExportAttribute.cs</Link>
5151
</Compile>
52+
<Compile Include="..\..\Visual Studio Project Template C#\Integration\ClikeStringArray.cs">
53+
<Link>Integration\ClikeStringArray.cs</Link>
54+
</Compile>
5255
<Compile Include="..\..\Visual Studio Project Template C#\ScintillaGateway.cs">
5356
<Link>ScintillaGateway.cs</Link>
5457
</Compile>

Visual Studio Project Template C#/$projectname$.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Reference Include="System.Windows.Forms" />
4343
</ItemGroup>
4444
<ItemGroup>
45+
<Compile Include="Integration\ClikeStringArray.cs" />
4546
<Compile Include="DllExport\DllExportAttribute.cs" />
4647
<Compile Include="Forms\frmMyDlg.cs">
4748
<SubType>Form</SubType>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
5+
namespace Kbg.NppPluginNET.Integration
6+
{
7+
public class ClikeStringArray : IDisposable
8+
{
9+
IntPtr _nativeArray;
10+
List<IntPtr> _nativeItems;
11+
bool _disposed = false;
12+
13+
public ClikeStringArray(int num, int stringCapacity)
14+
{
15+
_nativeArray = Marshal.AllocHGlobal((num + 1) * IntPtr.Size);
16+
_nativeItems = new List<IntPtr>();
17+
for (int i = 0; i < num; i++)
18+
{
19+
IntPtr item = Marshal.AllocHGlobal(stringCapacity);
20+
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
21+
_nativeItems.Add(item);
22+
}
23+
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (num * IntPtr.Size)), IntPtr.Zero);
24+
}
25+
public ClikeStringArray(List<string> lstStrings)
26+
{
27+
_nativeArray = Marshal.AllocHGlobal((lstStrings.Count + 1) * IntPtr.Size);
28+
_nativeItems = new List<IntPtr>();
29+
for (int i = 0; i < lstStrings.Count; i++)
30+
{
31+
IntPtr item = Marshal.StringToHGlobalUni(lstStrings[i]);
32+
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
33+
_nativeItems.Add(item);
34+
}
35+
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (lstStrings.Count * IntPtr.Size)), IntPtr.Zero);
36+
}
37+
38+
public IntPtr NativePointer { get { return _nativeArray; } }
39+
public List<string> ManagedStringsAnsi { get { return _getManagedItems(false); } }
40+
public List<string> ManagedStringsUnicode { get { return _getManagedItems(true); } }
41+
List<string> _getManagedItems(bool unicode)
42+
{
43+
List<string> _managedItems = new List<string>();
44+
for (int i = 0; i < _nativeItems.Count; i++)
45+
{
46+
if (unicode) _managedItems.Add(Marshal.PtrToStringUni(_nativeItems[i]));
47+
else _managedItems.Add(Marshal.PtrToStringAnsi(_nativeItems[i]));
48+
}
49+
return _managedItems;
50+
}
51+
52+
public void Dispose()
53+
{
54+
if (!_disposed)
55+
{
56+
for (int i = 0; i < _nativeItems.Count; i++)
57+
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
58+
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
59+
_disposed = true;
60+
}
61+
}
62+
~ClikeStringArray()
63+
{
64+
Dispose();
65+
}
66+
}
67+
}

Visual Studio Project Template C#/NppPlugin.vstemplate

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<ProjectItem ReplaceParameters="false" TargetFileName="MenuCmdID_h.cs">MenuCmdID_h.cs</ProjectItem>
4343
<ProjectItem ReplaceParameters="false" TargetFileName="Msgs_h.cs">Msgs_h.cs</ProjectItem>
4444
<ProjectItem ReplaceParameters="false" TargetFileName="Scintilla_iface.cs">Scintilla_iface.cs</ProjectItem>
45+
<ProjectItem ReplaceParameters="false" TargetFileName="ClikeStringArray.cs">ClikeStringArray.cs</ProjectItem>
4546
</Folder>
4647
<ProjectItem ReplaceParameters="true" TargetFileName="Main.cs" OpenInEditor="true">Main.cs</ProjectItem>
4748
<ProjectItem ReplaceParameters="false" TargetFileName="NppPluginNETBase.cs">NppPluginNETBase.cs</ProjectItem>

Visual Studio Project Template C#/NppPluginNETHelper.cs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -255,65 +255,5 @@ public class Win32
255255
public static extern void OutputDebugString(string lpOutputString);
256256
}
257257

258-
public class ClikeStringArray : IDisposable
259-
{
260-
IntPtr _nativeArray;
261-
List<IntPtr> _nativeItems;
262-
bool _disposed = false;
263-
264-
public ClikeStringArray(int num, int stringCapacity)
265-
{
266-
_nativeArray = Marshal.AllocHGlobal((num + 1) * IntPtr.Size);
267-
_nativeItems = new List<IntPtr>();
268-
for (int i = 0; i < num; i++)
269-
{
270-
IntPtr item = Marshal.AllocHGlobal(stringCapacity);
271-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
272-
_nativeItems.Add(item);
273-
}
274-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (num * IntPtr.Size)), IntPtr.Zero);
275-
}
276-
public ClikeStringArray(List<string> lstStrings)
277-
{
278-
_nativeArray = Marshal.AllocHGlobal((lstStrings.Count + 1) * IntPtr.Size);
279-
_nativeItems = new List<IntPtr>();
280-
for (int i = 0; i < lstStrings.Count; i++)
281-
{
282-
IntPtr item = Marshal.StringToHGlobalUni(lstStrings[i]);
283-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
284-
_nativeItems.Add(item);
285-
}
286-
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (lstStrings.Count * IntPtr.Size)), IntPtr.Zero);
287-
}
288-
289-
public IntPtr NativePointer { get { return _nativeArray; } }
290-
public List<string> ManagedStringsAnsi { get { return _getManagedItems(false); } }
291-
public List<string> ManagedStringsUnicode { get { return _getManagedItems(true); } }
292-
List<string> _getManagedItems(bool unicode)
293-
{
294-
List<string> _managedItems = new List<string>();
295-
for (int i = 0; i < _nativeItems.Count; i++)
296-
{
297-
if (unicode) _managedItems.Add(Marshal.PtrToStringUni(_nativeItems[i]));
298-
else _managedItems.Add(Marshal.PtrToStringAnsi(_nativeItems[i]));
299-
}
300-
return _managedItems;
301-
}
302-
303-
public void Dispose()
304-
{
305-
if (!_disposed)
306-
{
307-
for (int i = 0; i < _nativeItems.Count; i++)
308-
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
309-
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
310-
_disposed = true;
311-
}
312-
}
313-
~ClikeStringArray()
314-
{
315-
Dispose();
316-
}
317-
}
318-
#endregion
258+
#endregion
319259
}

0 commit comments

Comments
 (0)