-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added safe pointer class, arranged a bit the code.
-- Ready for 1.3 pre-release.
- Loading branch information
Showing
6 changed files
with
133 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace JLChnToZ.IMEHelper { | ||
public class SafePointer: IDisposable { | ||
private IntPtr _pointer; | ||
private int _length; | ||
private bool disposed; | ||
|
||
public IntPtr Pointer { get { return _pointer; } } | ||
|
||
public int Length { get { return _length; } } | ||
|
||
public SafePointer(int length) { | ||
_length = length; | ||
_pointer = Marshal.AllocHGlobal(_length); | ||
disposed = false; | ||
} | ||
|
||
~SafePointer() { | ||
Dispose(); | ||
} | ||
|
||
public void Dispose() { | ||
if (!disposed) { | ||
Marshal.FreeHGlobal(_pointer); | ||
disposed = true; | ||
} | ||
} | ||
|
||
public T ToStructure<T>() { | ||
return (T)Marshal.PtrToStructure(_pointer, typeof(T)); | ||
} | ||
|
||
public override string ToString() { | ||
return Marshal.PtrToStringAuto(_pointer); | ||
} | ||
|
||
public IntPtr GetOffsetPointer(int Offset) { | ||
return (IntPtr)(_pointer.ToInt32() + Offset); | ||
} | ||
|
||
public byte[] GetBytes(int start = 0) { | ||
byte[] b = new byte[_length - start]; | ||
Marshal.Copy(_pointer, b, start, _length - start); | ||
return b; | ||
} | ||
} | ||
} |