<#@ 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; } <# } #>