using Microsoft.Win32.SafeHandles; namespace WhiteMagic.Native; /// /// A Win32 handle (process, thread, or snapshot) with a managed lifetime. /// The handle closes with CloseHandle, even after an exception or a thread abort. /// /// The pattern comes from MemorySharp's SafeMemoryHandle. public sealed class SafeMemoryHandle : SafeHandleZeroOrMinusOneIsInvalid { /// /// Makes an empty handle. The interop marshaller uses this constructor for a /// handle that a system call returns (for example, ). /// public SafeMemoryHandle() : base(true) { } /// /// Wraps a raw handle and takes ownership of the handle. /// /// The handle to own. public SafeMemoryHandle(IntPtr handle) : base(true) { SetHandle(handle); } /// protected override bool ReleaseHandle() { return NativeMethods.CloseHandle(handle); } }