using System; using System.Runtime.ConstrainedExecution; namespace ScreenConnect; public class HandleMinder : CriticalFinalizerObject, IDisposable { public IntPtr Handle { get; private set; } public bool IsDisposed { get; private set; } public Proc DestroyProc { get; private set; } public override bool Equals(object obj) { HandleMinder handleMinder = obj as HandleMinder; if (handleMinder != null) { return handleMinder.Handle == Handle; } return false; } public override int GetHashCode() { return Handle.GetHashCode(); } public static bool operator ==(HandleMinder a, HandleMinder b) { return object.Equals(a, b); } public static bool operator !=(HandleMinder a, HandleMinder b) { return !object.Equals(a, b); } public static HandleMinder Create(IntPtr handle, params int[] allowabledErrorCodes) { if (handle == IntPtr.Zero) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = handle }; } public static HandleMinder CreateWithProc(IntPtr handle, Proc destroyProc, params int[] allowabledErrorCodes) { if (handle == IntPtr.Zero) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = handle, DestroyProc = destroyProc }; } public static HandleMinder CreateWithFunc(IntPtr handle, Func destroyProc, params int[] allowabledErrorCodes) { if (handle == IntPtr.Zero) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = handle, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static HandleMinder CreateWithProc(FuncOutFirst func, Proc destroyProc, T t, U u, params int[] allowabledErrorCodes) { if (!func(out var o, t, u)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = destroyProc }; } public static HandleMinder CreateWithFunc(FuncOutFirst func, Func destroyProc, T t, U u, params int[] allowabledErrorCodes) { if (!func(out var o, t, u)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static HandleMinder CreateWithProc(FuncOutLast func, Proc destroyProc, T t, U u, params int[] allowabledErrorCodes) { if (!func(t, u, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = destroyProc }; } public static HandleMinder CreateWithFunc(FuncOutLast func, Func destroyProc, T t, U u, params int[] allowabledErrorCodes) { if (!func(t, u, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static HandleMinder CreateWithProc(FuncOutLast func, Proc destroyProc, T t, U u, V v, params int[] allowabledErrorCodes) { if (!func(t, u, v, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = destroyProc }; } public static HandleMinder CreateWithFunc(FuncOutLast func, Func destroyProc, T t, U u, V v, params int[] allowabledErrorCodes) { if (!func(t, u, v, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static HandleMinder CreateWithProc(FuncOutLast func, Proc destroyProc, T t, U u, V v, W w, params int[] allowabledErrorCodes) { if (!func(t, u, v, w, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = destroyProc }; } public static HandleMinder CreateWithFunc(FuncOutLast func, Func destroyProc, T t, U u, V v, W w, params int[] allowabledErrorCodes) { if (!func(t, u, v, w, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static HandleMinder CreateWithIntFunc(FuncOutFirst func, Func destroyProc, T t, U u, V v, W w, X x, params int[] allowabledErrorCodes) { int num = func(out var o, t, u, v, w, x); if ((allowabledErrorCodes.Length == 0 && num != 0) || (allowabledErrorCodes.Length != 0 && Array.IndexOf(allowabledErrorCodes, num) != -1)) { throw WindowsExtensions.CreateWin32Exception(num); } return new HandleMinder { Handle = o, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static HandleMinder CreateWithIntFunc(FuncOutLast func, Func destroyProc, T t, U u, V v, W w, params int[] allowabledErrorCodes) { int num = func(t, u, v, w, out var o); if ((allowabledErrorCodes.Length == 0 && num != 0) || (allowabledErrorCodes.Length != 0 && Array.IndexOf(allowabledErrorCodes, num) != -1)) { throw WindowsExtensions.CreateWin32Exception(num); } return new HandleMinder { Handle = o, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static HandleMinder CreateWithProc(FuncOutLast func, Proc destroyProc, T t, U u, V v, W w, X x, params int[] allowabledErrorCodes) { if (!func(t, u, v, w, x, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = destroyProc }; } public static HandleMinder CreateWithFunc(FuncOutLast func, Func destroyProc, T t, U u, V v, W w, X x, params int[] allowabledErrorCodes) { if (!func(t, u, v, w, x, out var o)) { WindowsExtensions.ThrowLastError(allowabledErrorCodes); return null; } return new HandleMinder { Handle = o, DestroyProc = delegate(IntPtr h) { destroyProc(h); } }; } public static implicit operator IntPtr(HandleMinder hm) { if (!(hm == null)) { return hm.Handle; } return IntPtr.Zero; } ~HandleMinder() { if (!IsDisposed) { Dispose(disposing: false); } } public void Dispose() { if (!IsDisposed) { Dispose(disposing: true); GC.SuppressFinalize(this); } } protected void Dispose(bool disposing) { if (DestroyProc != null) { DestroyProc(Handle); } IsDisposed = true; } }