Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Runtime.InteropServices;
- namespace T4
- {
- public class NativeBuffer : IDisposable
- {
- private IntPtr hglobal;
- private bool disposed = false;
- public IntPtr Handle { get { return hglobal; } }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposed) return;
- if (disposing)
- {
- try
- {
- if (hglobal != IntPtr.Zero)
- Marshal.FreeHGlobal(hglobal);
- }
- catch
- {
- }
- }
- //here clean uncontrol
- disposed = true;
- }
- ~NativeBuffer()
- {
- Dispose(false);
- }
- public bool AllocHGlobal(int cb)
- {
- try
- {
- if (hglobal == IntPtr.Zero)
- hglobal = Marshal.AllocHGlobal(cb);
- return true;
- }
- catch
- {
- return false;
- }
- }
- public bool FreeHGlobal()
- {
- try
- {
- if (hglobal != IntPtr.Zero)
- Marshal.FreeHGlobal(hglobal);
- return true;
- }
- catch
- {
- return false;
- }
- }
- public bool IsAllocked()
- {
- return hglobal != IntPtr.Zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement