From 677081358a39e394a853ae77788d78af5ddba54e Mon Sep 17 00:00:00 2001 From: dahall Date: Thu, 21 Jul 2022 09:20:46 -0600 Subject: [PATCH] Refactored base handle structures into templates --- PInvoke/Shared/Handles/HANDLE.cs | 2413 +++++++++++++++++++++++++++ PInvoke/Shared/Handles/HANDLE.tt | 229 +++ PInvoke/Shared/Handles/IHandle.cs | 2305 +------------------------ PInvoke/Shared/Handles/SafeHANDLE.cs | 124 ++ PInvoke/Shared/Vanara.PInvoke.Shared.csproj | 23 + 5 files changed, 2811 insertions(+), 2283 deletions(-) create mode 100644 PInvoke/Shared/Handles/HANDLE.cs create mode 100644 PInvoke/Shared/Handles/HANDLE.tt create mode 100644 PInvoke/Shared/Handles/SafeHANDLE.cs diff --git a/PInvoke/Shared/Handles/HANDLE.cs b/PInvoke/Shared/Handles/HANDLE.cs new file mode 100644 index 00000000..b79cf256 --- /dev/null +++ b/PInvoke/Shared/Handles/HANDLE.cs @@ -0,0 +1,2413 @@ +using Microsoft.Win32.SafeHandles; +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace Vanara.PInvoke; + +/// Provides a handle to an accelerator table. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HACCEL : IUserHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HACCEL(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HACCEL NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HACCEL h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HACCEL(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HACCEL hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HACCEL h1, HACCEL h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HACCEL h1, HACCEL h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HACCEL h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a generic handle. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HANDLE : IHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HANDLE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HANDLE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HANDLE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HANDLE(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HANDLE(SafeHandle h) => new(h.DangerousGetHandle()); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HANDLE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HANDLE h1, HANDLE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HANDLE h1, HANDLE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HANDLE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a bitmap. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HBITMAP : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HBITMAP(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HBITMAP NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HBITMAP h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HBITMAP(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HBITMAP(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HBITMAP hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HBITMAP h1, HBITMAP h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HBITMAP h1, HBITMAP h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HBITMAP h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to drawing brush. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HBRUSH : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HBRUSH(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HBRUSH NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HBRUSH h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HBRUSH(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HBRUSH(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HBRUSH hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HBRUSH h1, HBRUSH h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HBRUSH h1, HBRUSH h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HBRUSH h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a color space. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HCOLORSPACE : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HCOLORSPACE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HCOLORSPACE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HCOLORSPACE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HCOLORSPACE(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HCOLORSPACE(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HCOLORSPACE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HCOLORSPACE h1, HCOLORSPACE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HCOLORSPACE h1, HCOLORSPACE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HCOLORSPACE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to cursor. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HCURSOR : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HCURSOR(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HCURSOR NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HCURSOR h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HCURSOR(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HCURSOR(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HCURSOR hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HCURSOR h1, HCURSOR h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HCURSOR h1, HCURSOR h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HCURSOR h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a graphic device context. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HDC : IHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HDC(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HDC NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HDC h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HDC(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HDC hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HDC h1, HDC h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HDC h1, HDC h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HDC h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a desktop. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HDESK : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HDESK(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HDESK NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HDESK h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HDESK(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HDESK hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HDESK h1, HDESK h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HDESK h1, HDESK h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HDESK h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a DPA. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HDPA : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HDPA(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HDPA NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HDPA h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HDPA(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HDPA hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HDPA h1, HDPA h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HDPA h1, HDPA h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HDPA h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a Windows drop operation. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HDROP : IShellHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HDROP(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HDROP NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HDROP h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HDROP(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HDROP hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HDROP h1, HDROP h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HDROP h1, HDROP h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HDROP h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a DSA. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HDSA : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HDSA(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HDSA NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HDSA h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HDSA(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HDSA hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HDSA h1, HDSA h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HDSA h1, HDSA h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HDSA h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a deferred windows position. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HDWP : IUserHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HDWP(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HDWP NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HDWP h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HDWP(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HDWP hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HDWP h1, HDWP h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HDWP h1, HDWP h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HDWP h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to an enhanced metafile. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HENHMETAFILE : IHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HENHMETAFILE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HENHMETAFILE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HENHMETAFILE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HENHMETAFILE(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HENHMETAFILE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HENHMETAFILE h1, HENHMETAFILE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HENHMETAFILE h1, HENHMETAFILE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HENHMETAFILE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a sync event. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HEVENT : ISyncHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HEVENT(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HEVENT NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HEVENT h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HEVENT(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HEVENT hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HEVENT h1, HEVENT h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HEVENT h1, HEVENT h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HEVENT h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a file. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HFILE : ISyncHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HFILE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HFILE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Represents an invalid handle. + public static readonly HFILE INVALID_HANDLE_VALUE = new IntPtr(-1); + + /// Gets a value indicating whether this instance is an invalid handle (INVALID_HANDLE_VALUE). + public bool IsInvalid => handle == INVALID_HANDLE_VALUE; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HFILE(SafeFileHandle h) => new(h?.DangerousGetHandle() ?? IntPtr.Zero); + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HFILE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HFILE(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HFILE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HFILE h1, HFILE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HFILE h1, HFILE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HFILE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a font. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HFONT : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HFONT(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HFONT NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HFONT h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HFONT(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HFONT(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HFONT hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HFONT h1, HFONT h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HFONT h1, HFONT h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HFONT h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a graphic device object. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HGDIOBJ : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HGDIOBJ(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HGDIOBJ NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HGDIOBJ h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HBITMAP h) => new((IntPtr)h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HBRUSH h) => new((IntPtr)h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HCOLORSPACE h) => new((IntPtr)h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HCURSOR h) => new((IntPtr)h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HFONT h) => new((IntPtr)h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HPALETTE h) => new((IntPtr)h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HPEN h) => new((IntPtr)h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(HRGN h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HGDIOBJ hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HGDIOBJ h1, HGDIOBJ h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HGDIOBJ h1, HGDIOBJ h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HGDIOBJ h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to an icon. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HICON : IUserHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HICON(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HICON NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HICON h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HICON(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HICON hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HICON h1, HICON h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HICON h1, HICON h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HICON h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a Windows image list. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HIMAGELIST : IShellHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HIMAGELIST(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HIMAGELIST NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HIMAGELIST h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HIMAGELIST(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HIMAGELIST hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HIMAGELIST h1, HIMAGELIST h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HIMAGELIST h1, HIMAGELIST h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HIMAGELIST h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a module or library instance. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HINSTANCE : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HINSTANCE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HINSTANCE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HINSTANCE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HINSTANCE(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HINSTANCE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HINSTANCE h1, HINSTANCE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HINSTANCE h1, HINSTANCE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HINSTANCE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a Windows registry key. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HKEY : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HKEY(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HKEY NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// + /// Registry entries subordinate to this key define types (or classes) of documents and the properties associated with those types. + /// Shell and COM applications use the information stored under this key. + /// + public static readonly HKEY HKEY_CLASSES_ROOT = new(new IntPtr(unchecked((int)0x80000000))); + + /// + /// Contains information about the current hardware profile of the local computer system. The information under HKEY_CURRENT_CONFIG + /// describes only the differences between the current hardware configuration and the standard configuration. Information about the + /// standard hardware configuration is stored under the Software and System keys of HKEY_LOCAL_MACHINE. + /// + public static readonly HKEY HKEY_CURRENT_CONFIG = new(new IntPtr(unchecked((int)0x80000005))); + + /// + /// Registry entries subordinate to this key define the preferences of the current user. These preferences include the settings of + /// environment variables, data about program groups, colors, printers, network connections, and application preferences. This key + /// makes it easier to establish the current user's settings; the key maps to the current user's branch in HKEY_USERS. In + /// HKEY_CURRENT_USER, software vendors store the current user-specific preferences to be used within their applications. Microsoft, + /// for example, creates the HKEY_CURRENT_USER\Software\Microsoft key for its applications to use, with each application creating its + /// own subkey under the Microsoft key. + /// + public static readonly HKEY HKEY_CURRENT_USER = new(new IntPtr(unchecked((int)0x80000001))); + + /// + public static readonly HKEY HKEY_DYN_DATA = new(new IntPtr(unchecked((int)0x80000006))); + + /// + /// Registry entries subordinate to this key define the physical state of the computer, including data about the bus type, system + /// memory, and installed hardware and software. It contains subkeys that hold current configuration data, including Plug and Play + /// information (the Enum branch, which includes a complete list of all hardware that has ever been on the system), network logon + /// preferences, network security information, software-related information (such as server names and the location of the server), + /// and other system information. + /// + public static readonly HKEY HKEY_LOCAL_MACHINE = new(new IntPtr(unchecked((int)0x80000002))); + + /// + /// Registry entries subordinate to this key allow you to access performance data. The data is not actually stored in the registry; + /// the registry functions cause the system to collect the data from its source. + /// + public static readonly HKEY HKEY_PERFORMANCE_DATA = new(new IntPtr(unchecked((int)0x80000004))); + + /// + /// Registry entries subordinate to this key define the default user configuration for new users on the local computer and the user + /// configuration for the current user. + /// + public static readonly HKEY HKEY_USERS = new(new IntPtr(unchecked((int)0x80000003))); + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HKEY h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HKEY(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HKEY(SafeRegistryHandle h) => new(h.DangerousGetHandle()); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HKEY hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HKEY h1, HKEY h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HKEY h1, HKEY h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HKEY h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a menu. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HMENU : IUserHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HMENU(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HMENU NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HMENU h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HMENU(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HMENU hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HMENU h1, HMENU h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HMENU h1, HMENU h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HMENU h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a metafile. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HMETAFILE : IHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HMETAFILE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HMETAFILE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HMETAFILE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HMETAFILE(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HMETAFILE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HMETAFILE h1, HMETAFILE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HMETAFILE h1, HMETAFILE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HMETAFILE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a monitor. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HMONITOR : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HMONITOR(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HMONITOR NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HMONITOR h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HMONITOR(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HMONITOR hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HMONITOR h1, HMONITOR h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HMONITOR h1, HMONITOR h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HMONITOR h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a palette. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HPALETTE : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HPALETTE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HPALETTE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HPALETTE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HPALETTE(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HPALETTE(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HPALETTE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HPALETTE h1, HPALETTE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HPALETTE h1, HPALETTE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HPALETTE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a drawing pen. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HPEN : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HPEN(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HPEN NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HPEN h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HPEN(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HPEN(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HPEN hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HPEN h1, HPEN h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HPEN h1, HPEN h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HPEN h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a process. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HPROCESS : ISyncHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HPROCESS(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HPROCESS NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HPROCESS h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HPROCESS(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HPROCESS(Process h) => new(h.Handle); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HPROCESS hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HPROCESS h1, HPROCESS h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HPROCESS h1, HPROCESS h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HPROCESS h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a Windows property sheet. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HPROPSHEET : IUserHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HPROPSHEET(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HPROPSHEET NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HPROPSHEET h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HPROPSHEET(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HPROPSHEET hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HPROPSHEET h1, HPROPSHEET h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HPROPSHEET h1, HPROPSHEET h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HPROPSHEET h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a property sheet page. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HPROPSHEETPAGE : IUserHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HPROPSHEETPAGE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HPROPSHEETPAGE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HPROPSHEETPAGE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HPROPSHEETPAGE(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HPROPSHEETPAGE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HPROPSHEETPAGE h1, HPROPSHEETPAGE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HPROPSHEETPAGE h1, HPROPSHEETPAGE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HPROPSHEETPAGE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a drawing region. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HRGN : IGraphicsObjectHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HRGN(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HRGN NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HRGN h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HRGN(IntPtr h) => new(h); + + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator HRGN(HGDIOBJ h) => new((IntPtr)h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HRGN hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HRGN h1, HRGN h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HRGN h1, HRGN h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HRGN h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a file mapping object. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HSECTION : IHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HSECTION(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HSECTION NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HSECTION h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HSECTION(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HSECTION hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HSECTION h1, HSECTION h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HSECTION h1, HSECTION h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HSECTION h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a blocking task. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HTASK : IHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HTASK(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HTASK NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HTASK h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HTASK(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HTASK hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HTASK h1, HTASK h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HTASK h1, HTASK h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HTASK h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a Windows theme. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HTHEME : IHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HTHEME(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HTHEME NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HTHEME h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HTHEME(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HTHEME hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HTHEME h1, HTHEME h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HTHEME h1, HTHEME h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HTHEME h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a thread. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HTHREAD : ISyncHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HTHREAD(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HTHREAD NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HTHREAD h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HTHREAD(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HTHREAD hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HTHREAD h1, HTHREAD h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HTHREAD h1, HTHREAD h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HTHREAD h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a Windows thumbnail. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HTHUMBNAIL : IShellHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HTHUMBNAIL(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HTHUMBNAIL NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HTHUMBNAIL h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HTHUMBNAIL(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HTHUMBNAIL hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HTHUMBNAIL h1, HTHUMBNAIL h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HTHUMBNAIL h1, HTHUMBNAIL h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HTHUMBNAIL h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to an access token. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HTOKEN : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HTOKEN(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HTOKEN NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HTOKEN h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HTOKEN(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HTOKEN hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HTOKEN h1, HTOKEN h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HTOKEN h1, HTOKEN h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HTOKEN h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a windows station. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HWINSTA : IKernelHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HWINSTA(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HWINSTA NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HWINSTA h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HWINSTA(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HWINSTA hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HWINSTA h1, HWINSTA h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HWINSTA h1, HWINSTA h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HWINSTA h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a window or dialog. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct HWND : IUserHandle +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public HWND(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static HWND NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// + /// Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost + /// status and is placed at the bottom of all other windows. + /// + public static HWND HWND_BOTTOM = new IntPtr(1); + + /// + /// Used by SendMessage and PostMessage to send a message to all top-level windows in the system, including disabled or + /// invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows. + /// + public static HWND HWND_BROADCAST = new IntPtr(0xffff); + + /// Use as parent in CreateWindow or CreateWindowEx call to indicate a message-only window. + public static HWND HWND_MESSAGE = new IntPtr(-3); + + /// + /// Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is + /// already a non-topmost window. + /// + public static HWND HWND_NOTOPMOST = new IntPtr(-2); + + /// Places the window at the top of the Z order. + public static HWND HWND_TOP = new IntPtr(0); + + /// Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated. + public static HWND HWND_TOPMOST = new IntPtr(-1); + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(HWND h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HWND(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(HWND hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(HWND h1, HWND h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(HWND h1, HWND h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is HWND h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a pointer to an access control entry. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct PACE : ISecurityObject +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public PACE(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static PACE NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(PACE h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator PACE(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(PACE hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(PACE h1, PACE h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(PACE h1, PACE h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is PACE h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to an access control list. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct PACL : ISecurityObject +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public PACL(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static PACL NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(PACL h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator PACL(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(PACL hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(PACL h1, PACL h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(PACL h1, PACL h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is PACL h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a security descriptor. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct PSECURITY_DESCRIPTOR : ISecurityObject +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public PSECURITY_DESCRIPTOR(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static PSECURITY_DESCRIPTOR NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(PSECURITY_DESCRIPTOR h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator PSECURITY_DESCRIPTOR(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(PSECURITY_DESCRIPTOR hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(PSECURITY_DESCRIPTOR h1, PSECURITY_DESCRIPTOR h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(PSECURITY_DESCRIPTOR h1, PSECURITY_DESCRIPTOR h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is PSECURITY_DESCRIPTOR h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +/// Provides a handle to a security identifier. +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct PSID : ISecurityObject +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public PSID(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static PSID NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(PSID h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator PSID(IntPtr h) => new(h); + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(PSID hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(PSID h1, PSID h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(PSID h1, PSID h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is PSID h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + diff --git a/PInvoke/Shared/Handles/HANDLE.tt b/PInvoke/Shared/Handles/HANDLE.tt new file mode 100644 index 00000000..a680e673 --- /dev/null +++ b/PInvoke/Shared/Handles/HANDLE.tt @@ -0,0 +1,229 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# var classes = new List<(string name, string inheritance, string summaryText)> { + ( "HACCEL", "IUserHandle", "Provides a handle to an accelerator table." ), + ( "HANDLE", "IHandle", "Provides a generic handle." ), + ( "HBITMAP", "IGraphicsObjectHandle", "Provides a handle to a bitmap." ), + ( "HBRUSH", "IGraphicsObjectHandle", "Provides a handle to drawing brush." ), + ( "HCOLORSPACE", "IGraphicsObjectHandle", "Provides a handle to a color space." ), + ( "HCURSOR", "IGraphicsObjectHandle", "Provides a handle to cursor." ), + ( "HDC", "IHandle", "Provides a handle to a graphic device context." ), + ( "HDESK", "IKernelHandle", "Provides a handle to a desktop." ), + ( "HDPA", "IKernelHandle", "Provides a handle to a DPA." ), + ( "HDROP", "IShellHandle", "Provides a handle to a Windows drop operation." ), + ( "HDSA", "IKernelHandle", "Provides a handle to a DSA." ), + ( "HDWP", "IUserHandle", "Provides a handle to a deferred windows position." ), + ( "HENHMETAFILE", "IHandle", "Provides a handle to an enhanced metafile." ), + ( "HEVENT", "ISyncHandle", "Provides a handle to a sync event." ), + ( "HFILE", "ISyncHandle", "Provides a handle to a file." ), + ( "HFONT", "IGraphicsObjectHandle", "Provides a handle to a font." ), + ( "HGDIOBJ", "IGraphicsObjectHandle", "Provides a handle to a graphic device object." ), // implicit ops from other handles + ( "HICON", "IUserHandle", "Provides a handle to an icon." ), + ( "HIMAGELIST", "IShellHandle", "Provides a handle to a Windows image list." ), + ( "HINSTANCE", "IKernelHandle", "Provides a handle to a module or library instance." ), + ( "HKEY", "IKernelHandle", "Provides a handle to a Windows registry key." ), + ( "HMENU", "IUserHandle", "Provides a handle to a menu." ), + ( "HMETAFILE", "IHandle", "Provides a handle to a metafile." ), + ( "HMONITOR", "IKernelHandle", "Provides a handle to a monitor." ), + ( "HPALETTE", "IGraphicsObjectHandle", "Provides a handle to a palette." ), + ( "HPEN", "IGraphicsObjectHandle", "Provides a handle to a drawing pen." ), + ( "HPROCESS", "ISyncHandle", "Provides a handle to a process." ), // implicit Process + ( "HPROPSHEET", "IUserHandle", "Provides a handle to a Windows property sheet." ), + ( "HPROPSHEETPAGE", "IUserHandle", "Provides a handle to a property sheet page." ), + ( "HRGN", "IGraphicsObjectHandle", "Provides a handle to a drawing region." ), + ( "HSECTION", "IHandle", "Provides a handle to a file mapping object." ), + ( "HTASK", "IHandle", "Provides a handle to a blocking task." ), + ( "HTHEME", "IHandle", "Provides a handle to a Windows theme." ), + ( "HTHREAD", "ISyncHandle", "Provides a handle to a thread." ), + ( "HTHUMBNAIL", "IShellHandle", "Provides a handle to a Windows thumbnail." ), + ( "HTOKEN", "IKernelHandle", "Provides a handle to an access token." ), + ( "HWINSTA", "IKernelHandle", "Provides a handle to a windows station." ), + ( "HWND", "IUserHandle", "Provides a handle to a window or dialog." ), // constants + ( "PACE", "ISecurityObject", "Provides a pointer to an access control entry." ), + ( "PACL", "ISecurityObject", "Provides a handle to an access control list." ), + ( "PSECURITY_DESCRIPTOR", "ISecurityObject", "Provides a handle to a security descriptor." ), + ( "PSID", "ISecurityObject", "Provides a handle to a security identifier." ), +}; #> +<# var casts = new Dictionary { + { "HANDLE", ("HANDLE", "SafeHandle", "h.DangerousGetHandle()" ) }, + { "HKEY", ("HKEY", "SafeRegistryHandle", "h.DangerousGetHandle()" ) }, + { "HPROCESS", ("HPROCESS", "Process", "h.Handle" ) }, +}; #> +using Microsoft.Win32.SafeHandles; +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace Vanara.PInvoke; + +<# foreach (var (name, inheritance, summaryText) in classes) { #> +/// <#= summaryText #> +[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] +public struct <#= name #> : <#= inheritance #> +{ + private readonly IntPtr handle; + + /// Initializes a new instance of the struct. + /// An object that represents the pre-existing handle to use. + public <#= name #>(IntPtr preexistingHandle) => handle = preexistingHandle; + + /// Returns an invalid handle by instantiating a object with . + public static <#= name #> NULL => new(IntPtr.Zero); + + /// Gets a value indicating whether this instance is a null handle. + public bool IsNull => handle == IntPtr.Zero; + +<# if (name == "HFILE") { #> + /// Represents an invalid handle. + public static readonly HFILE INVALID_HANDLE_VALUE = new IntPtr(-1); + + /// Gets a value indicating whether this instance is an invalid handle (INVALID_HANDLE_VALUE). + public bool IsInvalid => handle == INVALID_HANDLE_VALUE; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HFILE(SafeFileHandle h) => new(h?.DangerousGetHandle() ?? IntPtr.Zero); + +<# } #> +<# if (name == "HWND") { #> + /// + /// Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost + /// status and is placed at the bottom of all other windows. + /// + public static HWND HWND_BOTTOM = new IntPtr(1); + + /// + /// Used by SendMessage and PostMessage to send a message to all top-level windows in the system, including disabled or + /// invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows. + /// + public static HWND HWND_BROADCAST = new IntPtr(0xffff); + + /// Use as parent in CreateWindow or CreateWindowEx call to indicate a message-only window. + public static HWND HWND_MESSAGE = new IntPtr(-3); + + /// + /// Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is + /// already a non-topmost window. + /// + public static HWND HWND_NOTOPMOST = new IntPtr(-2); + + /// Places the window at the top of the Z order. + public static HWND HWND_TOP = new IntPtr(0); + + /// Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated. + public static HWND HWND_TOPMOST = new IntPtr(-1); + +<# } #> +<# if (name == "HKEY") { #> + /// + /// Registry entries subordinate to this key define types (or classes) of documents and the properties associated with those types. + /// Shell and COM applications use the information stored under this key. + /// + public static readonly HKEY HKEY_CLASSES_ROOT = new(new IntPtr(unchecked((int)0x80000000))); + + /// + /// Contains information about the current hardware profile of the local computer system. The information under HKEY_CURRENT_CONFIG + /// describes only the differences between the current hardware configuration and the standard configuration. Information about the + /// standard hardware configuration is stored under the Software and System keys of HKEY_LOCAL_MACHINE. + /// + public static readonly HKEY HKEY_CURRENT_CONFIG = new(new IntPtr(unchecked((int)0x80000005))); + + /// + /// Registry entries subordinate to this key define the preferences of the current user. These preferences include the settings of + /// environment variables, data about program groups, colors, printers, network connections, and application preferences. This key + /// makes it easier to establish the current user's settings; the key maps to the current user's branch in HKEY_USERS. In + /// HKEY_CURRENT_USER, software vendors store the current user-specific preferences to be used within their applications. Microsoft, + /// for example, creates the HKEY_CURRENT_USER\Software\Microsoft key for its applications to use, with each application creating its + /// own subkey under the Microsoft key. + /// + public static readonly HKEY HKEY_CURRENT_USER = new(new IntPtr(unchecked((int)0x80000001))); + + /// + public static readonly HKEY HKEY_DYN_DATA = new(new IntPtr(unchecked((int)0x80000006))); + + /// + /// Registry entries subordinate to this key define the physical state of the computer, including data about the bus type, system + /// memory, and installed hardware and software. It contains subkeys that hold current configuration data, including Plug and Play + /// information (the Enum branch, which includes a complete list of all hardware that has ever been on the system), network logon + /// preferences, network security information, software-related information (such as server names and the location of the server), + /// and other system information. + /// + public static readonly HKEY HKEY_LOCAL_MACHINE = new(new IntPtr(unchecked((int)0x80000002))); + + /// + /// Registry entries subordinate to this key allow you to access performance data. The data is not actually stored in the registry; + /// the registry functions cause the system to collect the data from its source. + /// + public static readonly HKEY HKEY_PERFORMANCE_DATA = new(new IntPtr(unchecked((int)0x80000004))); + + /// + /// Registry entries subordinate to this key define the default user configuration for new users on the local computer and the user + /// configuration for the current user. + /// + public static readonly HKEY HKEY_USERS = new(new IntPtr(unchecked((int)0x80000003))); + +<# } #> + /// Performs an explicit conversion from to . + /// The handle. + /// The result of the conversion. + public static explicit operator IntPtr(<#= name #> h) => h.handle; + + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator <#= name #>(IntPtr h) => new(h); + +<# if (casts.TryGetValue(name, out var value)) { #> + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator <#= value.Item1 #>(<#= value.Item2 #> h) => new(<#= value.Item3 #>); + +<# } #> +<# if (name == "HGDIOBJ") { foreach (var res in classes.Where(r => r.name != "HGDIOBJ" && r.inheritance == "IGraphicsObjectHandle")) {#> + /// Performs an implicit conversion from to . + /// The pointer to a handle. + /// The result of the conversion. + public static implicit operator HGDIOBJ(<#= res.name #> h) => new((IntPtr)h); + +<# } } #> +<# if (name != "HGDIOBJ" && inheritance == "IGraphicsObjectHandle") { #> + /// Performs an implicit conversion from to . + /// The pointer to a GDI handle. + /// The result of the conversion. + public static implicit operator <#= name #>(HGDIOBJ h) => new((IntPtr)h); + +<# } #> + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(<#= name #> hMem) => hMem.IsNull; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(<#= name #> h1, <#= name #> h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(<#= name #> h1, <#= name #> h2) => h1.Equals(h2); + + /// + public override bool Equals(object obj) => obj is <#= name #> h && handle == h.handle; + + /// + public override int GetHashCode() => handle.GetHashCode(); + + /// + public IntPtr DangerousGetHandle() => handle; +} + +<# } #> \ No newline at end of file diff --git a/PInvoke/Shared/Handles/IHandle.cs b/PInvoke/Shared/Handles/IHandle.cs index 5b1ebbc3..bb9bbf12 100644 --- a/PInvoke/Shared/Handles/IHandle.cs +++ b/PInvoke/Shared/Handles/IHandle.cs @@ -1,2290 +1,29 @@ -using Microsoft.Win32.SafeHandles; -using System; -using System.Diagnostics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using System; -namespace Vanara.PInvoke +namespace Vanara.PInvoke; + +/// Signals that a structure or class holds a handle to a synchronization object. +public interface IGraphicsObjectHandle : IUserHandle { } + +/// Signals that a structure or class holds a HANDLE. +public interface IHandle { - /// Signals that a structure or class holds a handle to a synchronization object. - public interface IGraphicsObjectHandle : IUserHandle - { } + /// Returns the value of the handle field. + /// An IntPtr representing the value of the handle field. + IntPtr DangerousGetHandle(); +} - /// Signals that a structure or class holds a HANDLE. - public interface IHandle - { - /// Returns the value of the handle field. - /// An IntPtr representing the value of the handle field. - IntPtr DangerousGetHandle(); - } +/// Signals that a structure or class holds a handle to a synchronization object. +public interface IKernelHandle : IHandle { } - /// Signals that a structure or class holds a handle to a synchronization object. - public interface IKernelHandle : IHandle - { } +/// Signals that a structure or class holds a pointer to a security object. +public interface ISecurityObject : IHandle { } - /// Signals that a structure or class holds a pointer to a security object. - public interface ISecurityObject : IHandle - { } +/// Signals that a structure or class holds a handle to a synchronization object. +public interface IShellHandle : IHandle { } - /// Signals that a structure or class holds a handle to a synchronization object. - public interface IShellHandle : IHandle - { } +/// Signals that a structure or class holds a handle to a synchronization object. +public interface ISyncHandle : IKernelHandle { } - /// Signals that a structure or class holds a handle to a synchronization object. - public interface ISyncHandle : IKernelHandle - { } - - /// Signals that a structure or class holds a handle to a synchronization object. - public interface IUserHandle : IHandle - { } - - /// Provides a handle to an accelerator table. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HACCEL : IUserHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HACCEL(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HACCEL NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HACCEL h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HACCEL(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HACCEL h1, HACCEL h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HACCEL h1, HACCEL h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HACCEL h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a generic handle. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HANDLE : IHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HANDLE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HANDLE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HANDLE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HANDLE(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The h. - /// The result of the conversion. - public static implicit operator HANDLE(SafeHandle h) => new(h.DangerousGetHandle()); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HANDLE h1, HANDLE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HANDLE h1, HANDLE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HANDLE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a bitmap. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HBITMAP : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HBITMAP(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HBITMAP NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HBITMAP h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HBITMAP(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a GDI handle. - /// The result of the conversion. - public static implicit operator HBITMAP(HGDIOBJ h) => new((IntPtr)h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HBITMAP h1, HBITMAP h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HBITMAP h1, HBITMAP h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HBITMAP h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a drawing brush. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HBRUSH : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HBRUSH(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HBRUSH NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HBRUSH h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HBRUSH(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a GDI handle. - /// The result of the conversion. - public static implicit operator HBRUSH(HGDIOBJ h) => new((IntPtr)h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HBRUSH h1, HBRUSH h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HBRUSH h1, HBRUSH h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HBRUSH h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a color space. - [StructLayout(LayoutKind.Sequential)] - public struct HCOLORSPACE : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HCOLORSPACE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HCOLORSPACE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HCOLORSPACE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HCOLORSPACE(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HCOLORSPACE h1, HCOLORSPACE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HCOLORSPACE h1, HCOLORSPACE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HCOLORSPACE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows cursor. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HCURSOR : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HCURSOR(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HCURSOR NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HCURSOR h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HCURSOR(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HCURSOR h1, HCURSOR h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HCURSOR h1, HCURSOR h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HCURSOR h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a graphic device context. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HDC : IHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HDC(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HDC NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HDC h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HDC(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HDC h1, HDC h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HDC h1, HDC h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HDC h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a desktop. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HDESK : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HDESK(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HDESK NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HDESK h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HDESK(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HDESK h1, HDESK h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HDESK h1, HDESK h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HDESK h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a DPA. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HDPA : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HDPA(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HDPA NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HDPA h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HDPA(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HDPA h1, HDPA h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HDPA h1, HDPA h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HDPA h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows drop operation. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HDROP : IShellHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HDROP(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HDROP NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HDROP h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HDROP(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HDROP h1, HDROP h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HDROP h1, HDROP h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HDROP h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a DSA. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HDSA : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HDSA(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HDSA NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HDSA h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HDSA(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HDSA h1, HDSA h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HDSA h1, HDSA h2) => h1.Equals(h2); - - /// Determines whether the specified , is equal to this instance. - /// The to compare with this instance. - /// true if the specified is equal to this instance; otherwise, false. - public override bool Equals(object obj) => obj is HDSA h && handle == h.handle; - - /// Returns a hash code for this instance. - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a deferred windows position. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HDWP : IUserHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HDWP(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HDWP NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HDWP h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HDWP(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HDWP h1, HDWP h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HDWP h1, HDWP h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HDWP h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to an enhanced metafile. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HENHMETAFILE : IHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HENHMETAFILE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HENHMETAFILE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HENHMETAFILE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HENHMETAFILE(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HENHMETAFILE h1, HENHMETAFILE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HENHMETAFILE h1, HENHMETAFILE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HENHMETAFILE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a synch event. - [StructLayout(LayoutKind.Sequential)] - public struct HEVENT : ISyncHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HEVENT(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HEVENT NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HEVENT h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HEVENT(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HEVENT h1, HEVENT h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HEVENT h1, HEVENT h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HEVENT h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a file. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HFILE : ISyncHandle - { - /// Represents an invalid handle. - public static readonly HFILE INVALID_HANDLE_VALUE = new IntPtr(-1); - - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HFILE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HFILE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is an invalid handle (INVALID_HANDLE_VALUE). - public bool IsInvalid => handle == INVALID_HANDLE_VALUE; - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HFILE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HFILE(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HFILE(SafeFileHandle h) => new(h?.DangerousGetHandle() ?? IntPtr.Zero); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HFILE h1, HFILE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HFILE h1, HFILE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HFILE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a font. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HFONT : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HFONT(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HFONT NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HFONT h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HFONT(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a GDI handle. - /// The result of the conversion. - public static implicit operator HFONT(HGDIOBJ h) => new((IntPtr)h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HFONT h1, HFONT h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HFONT h1, HFONT h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HFONT h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a graphic device object. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HGDIOBJ : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HGDIOBJ(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HGDIOBJ NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HGDIOBJ h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HBITMAP h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HBRUSH h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HCOLORSPACE h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HDC h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HFONT h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HMETAFILE h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HENHMETAFILE h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HPALETTE h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HPEN h) => new((IntPtr)h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HGDIOBJ(HRGN h) => new((IntPtr)h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HGDIOBJ h1, HGDIOBJ h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HGDIOBJ h1, HGDIOBJ h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HGDIOBJ h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows icon. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HICON : IUserHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HICON(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HICON NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HICON h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HICON(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HICON h1, HICON h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HICON h1, HICON h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HICON h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows image list. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HIMAGELIST : IShellHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HIMAGELIST(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HIMAGELIST NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HIMAGELIST h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HIMAGELIST(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HIMAGELIST h1, HIMAGELIST h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HIMAGELIST h1, HIMAGELIST h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HIMAGELIST h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a module or library instance. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HINSTANCE : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HINSTANCE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HINSTANCE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HINSTANCE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HINSTANCE(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HINSTANCE h1, HINSTANCE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HINSTANCE h1, HINSTANCE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HINSTANCE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows registry key. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HKEY : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HKEY(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HKEY NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// - /// Registry entries subordinate to this key define types (or classes) of documents and the properties associated with those types. - /// Shell and COM applications use the information stored under this key. - /// - public static readonly HKEY HKEY_CLASSES_ROOT = new(new IntPtr(unchecked((int)0x80000000))); - - /// - /// Contains information about the current hardware profile of the local computer system. The information under HKEY_CURRENT_CONFIG - /// describes only the differences between the current hardware configuration and the standard configuration. Information about the - /// standard hardware configuration is stored under the Software and System keys of HKEY_LOCAL_MACHINE. - /// - public static readonly HKEY HKEY_CURRENT_CONFIG = new(new IntPtr(unchecked((int)0x80000005))); - - /// - /// Registry entries subordinate to this key define the preferences of the current user. These preferences include the settings of - /// environment variables, data about program groups, colors, printers, network connections, and application preferences. This key - /// makes it easier to establish the current user's settings; the key maps to the current user's branch in HKEY_USERS. In - /// HKEY_CURRENT_USER, software vendors store the current user-specific preferences to be used within their applications. Microsoft, - /// for example, creates the HKEY_CURRENT_USER\Software\Microsoft key for its applications to use, with each application creating its - /// own subkey under the Microsoft key. - /// - public static readonly HKEY HKEY_CURRENT_USER = new(new IntPtr(unchecked((int)0x80000001))); - - /// - public static readonly HKEY HKEY_DYN_DATA = new(new IntPtr(unchecked((int)0x80000006))); - - /// - /// Registry entries subordinate to this key define the physical state of the computer, including data about the bus type, system - /// memory, and installed hardware and software. It contains subkeys that hold current configuration data, including Plug and Play - /// information (the Enum branch, which includes a complete list of all hardware that has ever been on the system), network logon - /// preferences, network security information, software-related information (such as server names and the location of the server), - /// and other system information. - /// - public static readonly HKEY HKEY_LOCAL_MACHINE = new(new IntPtr(unchecked((int)0x80000002))); - - /// - /// Registry entries subordinate to this key allow you to access performance data. The data is not actually stored in the registry; - /// the registry functions cause the system to collect the data from its source. - /// - public static readonly HKEY HKEY_PERFORMANCE_DATA = new(new IntPtr(unchecked((int)0x80000004))); - - /// - /// Registry entries subordinate to this key define the default user configuration for new users on the local computer and the user - /// configuration for the current user. - /// - public static readonly HKEY HKEY_USERS = new(new IntPtr(unchecked((int)0x80000003))); - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HKEY h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HKEY(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HKEY(SafeRegistryHandle h) => new(h.DangerousGetHandle()); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HKEY h1, HKEY h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HKEY h1, HKEY h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HKEY h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a menu. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HMENU : IUserHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HMENU(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HMENU NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HMENU h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HMENU(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HMENU h1, HMENU h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HMENU h1, HMENU h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HMENU h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a metafile. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HMETAFILE : IHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HMETAFILE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HMETAFILE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HMETAFILE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HMETAFILE(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HMETAFILE h1, HMETAFILE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HMETAFILE h1, HMETAFILE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HMETAFILE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a monitor. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HMONITOR : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HMONITOR(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HMONITOR NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HMONITOR h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HMONITOR(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HMONITOR h1, HMONITOR h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HMONITOR h1, HMONITOR h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HMONITOR h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a palette. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HPALETTE : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HPALETTE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HPALETTE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HPALETTE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HPALETTE(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a GDI handle. - /// The result of the conversion. - public static implicit operator HPALETTE(HGDIOBJ h) => new((IntPtr)h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HPALETTE h1, HPALETTE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HPALETTE h1, HPALETTE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HPALETTE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a drawing pen. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HPEN : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HPEN(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HPEN NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HPEN h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HPEN(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a GDI handle. - /// The result of the conversion. - public static implicit operator HPEN(HGDIOBJ h) => new((IntPtr)h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HPEN h1, HPEN h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HPEN h1, HPEN h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HPEN h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a process. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HPROCESS : ISyncHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HPROCESS(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HPROCESS NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HPROCESS h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HPROCESS(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The Process instance. - /// The result of the conversion. - public static implicit operator HPROCESS(Process p) => new(p.Handle); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HPROCESS h1, HPROCESS h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HPROCESS h1, HPROCESS h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HPROCESS h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows property sheet. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HPROPSHEET : IUserHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HPROPSHEET(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HPROPSHEET NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HPROPSHEET h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HPROPSHEET(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HPROPSHEET h1, HPROPSHEET h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HPROPSHEET h1, HPROPSHEET h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HPROPSHEET h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a property sheet page. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HPROPSHEETPAGE : IUserHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HPROPSHEETPAGE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HPROPSHEETPAGE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HPROPSHEETPAGE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HPROPSHEETPAGE(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HPROPSHEETPAGE h1, HPROPSHEETPAGE h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HPROPSHEETPAGE h1, HPROPSHEETPAGE h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HPROPSHEETPAGE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a drawing region. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HRGN : IGraphicsObjectHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HRGN(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HRGN NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HRGN h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HRGN(IntPtr h) => new(h); - - /// Performs an implicit conversion from to . - /// The pointer to a GDI handle. - /// The result of the conversion. - public static implicit operator HRGN(HGDIOBJ h) => new((IntPtr)h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HRGN h1, HRGN h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HRGN h1, HRGN h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HRGN h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a file mapping object. - [StructLayout(LayoutKind.Sequential)] - public struct HSECTION : IHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HSECTION(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HSECTION NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HSECTION h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HSECTION(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HSECTION h1, HSECTION h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HSECTION h1, HSECTION h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HSECTION h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a blocking task. - [StructLayout(LayoutKind.Sequential)] - public struct HTASK : IHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HTASK(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HTASK NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HTASK h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HTASK(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HTASK h1, HTASK h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HTASK h1, HTASK h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HTASK h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows theme. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HTHEME : IHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HTHEME(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HTHEME NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HTHEME h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HTHEME(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HTHEME h1, HTHEME h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HTHEME h1, HTHEME h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HTHEME h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a thread. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HTHREAD : ISyncHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HTHREAD(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HTHREAD NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HTHREAD h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HTHREAD(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HTHREAD h1, HTHREAD h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HTHREAD h1, HTHREAD h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HTHREAD h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a Windows thumbnail. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HTHUMBNAIL : IShellHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HTHUMBNAIL(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HTHUMBNAIL NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HTHUMBNAIL h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HTHUMBNAIL(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HTHUMBNAIL h1, HTHUMBNAIL h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HTHUMBNAIL h1, HTHUMBNAIL h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HTHUMBNAIL h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to an access token . - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HTOKEN : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HTOKEN(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HTOKEN NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HTOKEN h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HTOKEN(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HTOKEN h1, HTOKEN h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HTOKEN h1, HTOKEN h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HTOKEN h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a windows station. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HWINSTA : IKernelHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HWINSTA(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static HWINSTA NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HWINSTA h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HWINSTA(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HWINSTA h1, HWINSTA h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HWINSTA h1, HWINSTA h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HWINSTA h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a handle to a window or dialog. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HWND : IUserHandle - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public HWND(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// - /// Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost - /// status and is placed at the bottom of all other windows. - /// - public static HWND HWND_BOTTOM = new IntPtr(1); - - /// - /// Used by SendMessage and PostMessage to send a message to all top-level windows in the system, including disabled or - /// invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows. - /// - public static HWND HWND_BROADCAST = new IntPtr(0xffff); - - /// Use as parent in CreateWindow or CreateWindowEx call to indicate a message-only window. - public static HWND HWND_MESSAGE = new IntPtr(-3); - - /// - /// Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is - /// already a non-topmost window. - /// - public static HWND HWND_NOTOPMOST = new IntPtr(-2); - - /// Places the window at the top of the Z order. - public static HWND HWND_TOP = new IntPtr(0); - - /// Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated. - public static HWND HWND_TOPMOST = new IntPtr(-1); - - /// Returns an invalid handle by instantiating a object with . - public static HWND NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(HWND h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator HWND(IntPtr h) => new(h); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(HWND h1, HWND h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(HWND h1, HWND h2) => h1.Equals(h2); - - /// - public override bool Equals(object obj) => obj is HWND h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a pointer to an access control entry. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct PACE : ISecurityObject - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public PACE(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static PACE NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(PACE h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator PACE(IntPtr h) => new(h); - - /// - public override bool Equals(object obj) => obj is PACE h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a pointer to an access control list. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct PACL : ISecurityObject - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public PACL(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static PACL NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(PACL h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator PACL(IntPtr h) => new(h); - - /// - public override bool Equals(object obj) => obj is PACL h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a pointer to a security descriptor. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct PSECURITY_DESCRIPTOR : ISecurityObject - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public PSECURITY_DESCRIPTOR(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static PSECURITY_DESCRIPTOR NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(PSECURITY_DESCRIPTOR h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator PSECURITY_DESCRIPTOR(IntPtr h) => new(h); - - /// - public override bool Equals(object obj) => obj is PSECURITY_DESCRIPTOR h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Provides a pointer to a security identifier. - [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct PSID : ISecurityObject - { - private readonly IntPtr handle; - - /// Initializes a new instance of the struct. - /// An object that represents the pre-existing handle to use. - public PSID(IntPtr preexistingHandle) => handle = preexistingHandle; - - /// Returns an invalid handle by instantiating a object with . - public static PSID NULL => new(IntPtr.Zero); - - /// Gets a value indicating whether this instance is a null handle. - public bool IsNull => handle == IntPtr.Zero; - - /// Performs an explicit conversion from to . - /// The handle. - /// The result of the conversion. - public static explicit operator IntPtr(PSID h) => h.handle; - - /// Performs an implicit conversion from to . - /// The pointer to a handle. - /// The result of the conversion. - public static implicit operator PSID(IntPtr h) => new(h); - - /// - public override bool Equals(object obj) => obj is PSID h && handle == h.handle; - - /// - public override int GetHashCode() => handle.GetHashCode(); - - /// - public IntPtr DangerousGetHandle() => handle; - } - - /// Base class for all native handles. - /// - /// - /// - [DebuggerDisplay("{handle}")] - public abstract class SafeHANDLE : SafeHandleZeroOrMinusOneIsInvalid, IEquatable, IHandle - { - /// Initializes a new instance of the class. - public SafeHANDLE() : base(true) - { - } - - /// Initializes a new instance of the class and assigns an existing handle. - /// An object that represents the pre-existing handle to use. - /// - /// to reliably release the handle during the finalization phase; otherwise, (not recommended). - /// - protected SafeHANDLE(IntPtr preexistingHandle, bool ownsHandle = true) : base(ownsHandle) => SetHandle(preexistingHandle); - - /// Gets a value indicating whether this instance is null. - /// true if this instance is null; otherwise, false. - public bool IsNull => handle == IntPtr.Zero; - - /// Implements the operator ! which returns if the handle is invalid. - /// The instance. - /// The result of the operator. - public static bool operator !(SafeHANDLE hMem) => hMem.IsInvalid; - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(SafeHANDLE h1, IHandle h2) => !(h1 == h2); - - /// Implements the operator !=. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator !=(SafeHANDLE h1, IntPtr h2) => !(h1 == h2); - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(SafeHANDLE h1, IHandle h2) => h1?.Equals(h2) ?? h2 is null; - - /// Implements the operator ==. - /// The first handle. - /// The second handle. - /// The result of the operator. - public static bool operator ==(SafeHANDLE h1, IntPtr h2) => h1?.Equals(h2) ?? false; - - /// Determines whether the specified , is equal to this instance. - /// The to compare with this instance. - /// true if the specified is equal to this instance; otherwise, false. - public bool Equals(SafeHANDLE other) => ReferenceEquals(this, other) || other is not null && handle == other.handle && IsClosed == other.IsClosed; - - /// Determines whether the specified , is equal to this instance. - /// The to compare with this instance. - /// true if the specified is equal to this instance; otherwise, false. - public override bool Equals(object obj) => obj switch - { - IHandle ih => handle.Equals(ih.DangerousGetHandle()), - SafeHandle sh => handle.Equals(sh.DangerousGetHandle()), - IntPtr p => handle.Equals(p), - _ => base.Equals(obj), - }; - - /// Returns a hash code for this instance. - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - public override int GetHashCode() => base.GetHashCode(); - - /// Releases the ownership of the underlying handle and returns the current handle. - /// The value of the current handle. - public IntPtr ReleaseOwnership() - { - var ret = handle; - SetHandleAsInvalid(); - return ret; - } - - /// - /// Internal method that actually releases the handle. This is called by for valid handles and afterwards - /// zeros the handle. - /// - /// true to indicate successful release of the handle; false otherwise. - protected abstract bool InternalReleaseHandle(); - - /// - [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - protected override bool ReleaseHandle() - { - if (IsInvalid) return true; - if (!InternalReleaseHandle()) return false; - handle = IntPtr.Zero; - return true; - } - } -} \ No newline at end of file +/// Signals that a structure or class holds a handle to a synchronization object. +public interface IUserHandle : IHandle { } \ No newline at end of file diff --git a/PInvoke/Shared/Handles/SafeHANDLE.cs b/PInvoke/Shared/Handles/SafeHANDLE.cs new file mode 100644 index 00000000..b7f918ea --- /dev/null +++ b/PInvoke/Shared/Handles/SafeHANDLE.cs @@ -0,0 +1,124 @@ +using Microsoft.Win32.SafeHandles; +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace Vanara.PInvoke +{ + /// Base class for all native handles. + /// + /// + /// + [DebuggerDisplay("{handle}")] + public abstract class SafeHANDLE : SafeHandleZeroOrMinusOneIsInvalid, IEquatable, IHandle + { + /// Initializes a new instance of the class. + public SafeHANDLE() : base(true) + { + } + + /// Initializes a new instance of the class and assigns an existing handle. + /// An object that represents the pre-existing handle to use. + /// + /// to reliably release the handle during the finalization phase; otherwise, (not recommended). + /// + protected SafeHANDLE(IntPtr preexistingHandle, bool ownsHandle = true) : base(ownsHandle) => SetHandle(preexistingHandle); + + /// Gets a value indicating whether this instance is null. + /// true if this instance is null; otherwise, false. + public bool IsNull => handle == IntPtr.Zero; + + /// Implements the operator ! which returns if the handle is invalid. + /// The instance. + /// The result of the operator. + public static bool operator !(SafeHANDLE hMem) => hMem.IsInvalid; + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(SafeHANDLE h1, IHandle h2) => !(h1 == h2); + + /// Implements the operator !=. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator !=(SafeHANDLE h1, IntPtr h2) => !(h1 == h2); + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(SafeHANDLE h1, IHandle h2) => h1?.Equals(h2) ?? h2 is null; + + /// Implements the operator ==. + /// The first handle. + /// The second handle. + /// The result of the operator. + public static bool operator ==(SafeHANDLE h1, IntPtr h2) => h1?.Equals(h2) ?? false; + + /// Determines whether the specified , is equal to this instance. + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public bool Equals(SafeHANDLE other) => ReferenceEquals(this, other) || other is not null && handle == other.handle && IsClosed == other.IsClosed; + + /// Determines whether the specified , is equal to this instance. + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object obj) => obj switch + { + IHandle ih => handle.Equals(ih.DangerousGetHandle()), + SafeHandle sh => handle.Equals(sh.DangerousGetHandle()), + IntPtr p => handle.Equals(p), + _ => base.Equals(obj), + }; + + /// Returns a hash code for this instance. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() => base.GetHashCode(); + + /// Releases the ownership of the underlying handle and returns the current handle. + /// The value of the current handle. + public IntPtr ReleaseOwnership() + { + var ret = handle; + SetHandleAsInvalid(); + return ret; + } + + void X() + { + var classes = new System.Collections.Generic.List<(string name, string inheritance, string summaryText)> { + ( "LPSTR", "LPTSTR", "LPWSTR" ), + }; + + var casts = new System.Collections.Generic.Dictionary { + { "HANDLE", ("HANDLE", "SafeHandle" ) }, + { "HPROCESS", ("Process", "SafeHandle" ) }, + }; + + foreach (var (name, inheritance, summaryText) in classes) + { + + } + } + + /// + /// Internal method that actually releases the handle. This is called by for valid handles and afterwards + /// zeros the handle. + /// + /// true to indicate successful release of the handle; false otherwise. + protected abstract bool InternalReleaseHandle(); + + /// + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] + protected override bool ReleaseHandle() + { + if (IsInvalid) return true; + if (!InternalReleaseHandle()) return false; + handle = IntPtr.Zero; + return true; + } + } +} \ No newline at end of file diff --git a/PInvoke/Shared/Vanara.PInvoke.Shared.csproj b/PInvoke/Shared/Vanara.PInvoke.Shared.csproj index d742fdd5..75984f47 100644 --- a/PInvoke/Shared/Vanara.PInvoke.Shared.csproj +++ b/PInvoke/Shared/Vanara.PInvoke.Shared.csproj @@ -28,10 +28,33 @@ CharacterSet, CM_DEVCAP, CM_FILE, CM_INSTALL_STATE, CM_REMOVAL_POLICY, CM_RESOUR pkgreadme.md + + + True + True + HANDLE.tt + + + + + TextTemplatingFileGenerator + HANDLE.cs + + + + + + + + True + True + HANDLE.tt + + \ No newline at end of file