From fe562e8cf33b316ba2b259d870c84d03390d4f35 Mon Sep 17 00:00:00 2001 From: goaaats Date: Sat, 12 Apr 2025 22:20:55 +0200 Subject: [PATCH] Remove most hand-authored native functions --- .../Internal/DalamudConfiguration.cs | 15 +- Dalamud/Dalamud.cs | 43 +- Dalamud/Dalamud.csproj | 4 + Dalamud/EntryPoint.cs | 10 +- .../Game/Network/Internal/WinSockHandlers.cs | 33 + Dalamud/Hooking/Hook.cs | 10 +- .../Internal/Asserts/AssertHandler.cs | 33 +- .../Interface/Internal/InterfaceManager.cs | 11 +- .../IFontAtlasBuildToolkitPreBuild.cs | 6 +- .../Interface/ManagedFontAtlas/IFontHandle.cs | 4 +- Dalamud/Memory/MemoryHelper.cs | 45 +- Dalamud/NativeFunctions.cs | 1660 ----------------- Dalamud/NativeMethods.txt | 27 +- Dalamud/Utility/Util.cs | 47 +- 14 files changed, 204 insertions(+), 1744 deletions(-) delete mode 100644 Dalamud/NativeFunctions.cs diff --git a/Dalamud/Configuration/Internal/DalamudConfiguration.cs b/Dalamud/Configuration/Internal/DalamudConfiguration.cs index 515556b7e..24e2a4266 100644 --- a/Dalamud/Configuration/Internal/DalamudConfiguration.cs +++ b/Dalamud/Configuration/Internal/DalamudConfiguration.cs @@ -21,6 +21,7 @@ using Dalamud.Utility; using Newtonsoft.Json; using Serilog; using Serilog.Events; +using Windows.Win32.UI.WindowsAndMessaging; namespace Dalamud.Configuration.Internal; @@ -595,11 +596,15 @@ internal sealed class DalamudConfiguration : IInternalDisposableService { // https://source.chromium.org/chromium/chromium/src/+/main:ui/gfx/animation/animation_win.cc;l=29?q=ReducedMotion&ss=chromium var winAnimEnabled = 0; - var success = NativeFunctions.SystemParametersInfo( - (uint)NativeFunctions.AccessibilityParameter.SPI_GETCLIENTAREAANIMATION, - 0, - ref winAnimEnabled, - 0); + var success = false; + unsafe + { + success = Windows.Win32.PInvoke.SystemParametersInfo( + SYSTEM_PARAMETERS_INFO_ACTION.SPI_GETCLIENTAREAANIMATION, + 0, + &winAnimEnabled, + 0); + } if (!success) { diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 93de4c64d..b25094564 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -15,6 +15,7 @@ using Dalamud.Utility; using Dalamud.Utility.Timing; using PInvoke; using Serilog; +using Windows.Win32.Foundation; #if DEBUG [assembly: InternalsVisibleTo("Dalamud.CorePlugin")] @@ -29,13 +30,13 @@ namespace Dalamud; /// The main Dalamud class containing all subsystems. /// [ServiceManager.ProvidedService] -internal sealed class Dalamud : IServiceType +internal sealed unsafe class Dalamud : IServiceType { #region Internals private static int shownServiceError = 0; private readonly ManualResetEvent unloadSignal; - + #endregion /// @@ -48,15 +49,15 @@ internal sealed class Dalamud : IServiceType public Dalamud(DalamudStartInfo info, ReliableFileStorage fs, DalamudConfiguration configuration, IntPtr mainThreadContinueEvent) { this.StartInfo = info; - + this.unloadSignal = new ManualResetEvent(false); this.unloadSignal.Reset(); - + // Directory resolved signatures(CS, our own) will be cached in var cacheDir = new DirectoryInfo(Path.Combine(this.StartInfo.WorkingDirectory!, "cachedSigs")); if (!cacheDir.Exists) cacheDir.Create(); - + // Set up the SigScanner for our target module TargetSigScanner scanner; using (Timings.Start("SigScanner Init")) @@ -71,21 +72,21 @@ internal sealed class Dalamud : IServiceType configuration, scanner, Localization.FromAssets(info.AssetDirectory!, configuration.LanguageOverride)); - + // Set up FFXIVClientStructs this.SetupClientStructsResolver(cacheDir); - + void KickoffGameThread() { Log.Verbose("=============== GAME THREAD KICKOFF ==============="); Timings.Event("Game thread kickoff"); - NativeFunctions.SetEvent(mainThreadContinueEvent); + Windows.Win32.PInvoke.SetEvent(new HANDLE(mainThreadContinueEvent)); } void HandleServiceInitFailure(Task t) { Log.Error(t.Exception!, "Service initialization failure"); - + if (Interlocked.CompareExchange(ref shownServiceError, 1, 0) != 0) return; @@ -116,15 +117,15 @@ internal sealed class Dalamud : IServiceType HandleServiceInitFailure(t); }); - this.DefaultExceptionFilter = NativeFunctions.SetUnhandledExceptionFilter(nint.Zero); - NativeFunctions.SetUnhandledExceptionFilter(this.DefaultExceptionFilter); - Log.Debug($"SE default exception filter at {this.DefaultExceptionFilter.ToInt64():X}"); + this.DefaultExceptionFilter = SetExceptionHandler(nint.Zero); + SetExceptionHandler(this.DefaultExceptionFilter); + Log.Debug($"SE default exception filter at {new IntPtr(this.DefaultExceptionFilter):X}"); var debugSig = "40 55 53 57 48 8D AC 24 70 AD FF FF"; this.DebugExceptionFilter = Service.Get().ScanText(debugSig); Log.Debug($"SE debug exception filter at {this.DebugExceptionFilter.ToInt64():X}"); } - + /// /// Gets the start information for this Dalamud instance. /// @@ -188,28 +189,30 @@ internal sealed class Dalamud : IServiceType /// /// Replace the current exception handler with the default one. /// - internal void UseDefaultExceptionHandler() => - this.SetExceptionHandler(this.DefaultExceptionFilter); + internal void UseDefaultExceptionHandler() => + SetExceptionHandler(this.DefaultExceptionFilter); /// /// Replace the current exception handler with a debug one. /// internal void UseDebugExceptionHandler() => - this.SetExceptionHandler(this.DebugExceptionFilter); + SetExceptionHandler(this.DebugExceptionFilter); /// /// Disable the current exception handler. /// internal void UseNoExceptionHandler() => - this.SetExceptionHandler(nint.Zero); + SetExceptionHandler(nint.Zero); /// /// Helper function to set the exception handler. /// - private void SetExceptionHandler(nint newFilter) + private static nint SetExceptionHandler(nint newFilter) { - var oldFilter = NativeFunctions.SetUnhandledExceptionFilter(newFilter); - Log.Debug("Set ExceptionFilter to {0}, old: {1}", newFilter, oldFilter); + var oldFilter = + Windows.Win32.PInvoke.SetUnhandledExceptionFilter((delegate* unmanaged[Stdcall])newFilter); + Log.Debug("Set ExceptionFilter to {0}, old: {1}", newFilter, (nint)oldFilter); + return (nint)oldFilter; } private void SetupClientStructsResolver(DirectoryInfo cacheDir) diff --git a/Dalamud/Dalamud.csproj b/Dalamud/Dalamud.csproj index e5dd33226..5456d2fac 100644 --- a/Dalamud/Dalamud.csproj +++ b/Dalamud/Dalamud.csproj @@ -35,6 +35,10 @@ true annotations true + + + diff --git a/Dalamud/EntryPoint.cs b/Dalamud/EntryPoint.cs index 463ff1967..2592c5877 100644 --- a/Dalamud/EntryPoint.cs +++ b/Dalamud/EntryPoint.cs @@ -23,8 +23,6 @@ using Serilog.Events; using Windows.Win32.Foundation; using Windows.Win32.UI.WindowsAndMessaging; -using static Dalamud.NativeFunctions; - namespace Dalamud; /// @@ -264,10 +262,12 @@ public sealed class EntryPoint var symbolPath = Path.Combine(info.AssetDirectory, "UIRes", "pdb"); var searchPath = $".;{symbolPath}"; - // Remove any existing Symbol Handler and Init a new one with our search path added - SymCleanup(GetCurrentProcess()); + var currentProcess = Windows.Win32.PInvoke.GetCurrentProcess_SafeHandle(); - if (!SymInitialize(GetCurrentProcess(), searchPath, true)) + // Remove any existing Symbol Handler and Init a new one with our search path added + Windows.Win32.PInvoke.SymCleanup(currentProcess); + + if (!Windows.Win32.PInvoke.SymInitialize(currentProcess, searchPath, true)) throw new Win32Exception(); } catch (Exception ex) diff --git a/Dalamud/Game/Network/Internal/WinSockHandlers.cs b/Dalamud/Game/Network/Internal/WinSockHandlers.cs index 384858cfe..4b002021a 100644 --- a/Dalamud/Game/Network/Internal/WinSockHandlers.cs +++ b/Dalamud/Game/Network/Internal/WinSockHandlers.cs @@ -55,4 +55,37 @@ internal sealed class WinSockHandlers : IInternalDisposableService return socket; } + + /// + /// Native ws2_32 functions. + /// + private static class NativeFunctions + { + /// + /// See https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt. + /// The setsockopt function sets a socket option. + /// + /// + /// A descriptor that identifies a socket. + /// + /// + /// The level at which the option is defined (for example, SOL_SOCKET). + /// + /// + /// The socket option for which the value is to be set (for example, SO_BROADCAST). The optname parameter must be a + /// socket option defined within the specified level, or behavior is undefined. + /// + /// + /// A pointer to the buffer in which the value for the requested option is specified. + /// + /// + /// The size, in bytes, of the buffer pointed to by the optval parameter. + /// + /// + /// If no error occurs, setsockopt returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error + /// code can be retrieved by calling WSAGetLastError. + /// + [DllImport("ws2_32.dll", CallingConvention = CallingConvention.Winapi, EntryPoint = "setsockopt")] + public static extern int SetSockOpt(IntPtr socket, SocketOptionLevel level, SocketOptionName optName, ref IntPtr optVal, int optLen); + } } diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index 20796bbf0..51e62e918 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -70,12 +70,12 @@ public abstract class Hook : IDalamudHook where T : Delegate /// public virtual string BackendName => throw new NotImplementedException(); - + /// /// Gets the unique GUID for this hook. /// protected Guid HookId { get; } = Guid.NewGuid(); - + /// /// Remove a hook from the current process. /// @@ -200,11 +200,11 @@ public abstract class Hook : IDalamudHook where T : Delegate if (EnvironmentConfiguration.DalamudForceMinHook) useMinHook = true; - var moduleHandle = NativeFunctions.GetModuleHandleW(moduleName); - if (moduleHandle == IntPtr.Zero) + using var moduleHandle = Windows.Win32.PInvoke.GetModuleHandle(moduleName); + if (moduleHandle.IsInvalid) throw new Exception($"Could not get a handle to module {moduleName}"); - var procAddress = NativeFunctions.GetProcAddress(moduleHandle, exportName); + var procAddress = (nint)Windows.Win32.PInvoke.GetProcAddress(moduleHandle, exportName); if (procAddress == IntPtr.Zero) throw new Exception($"Could not get the address of {moduleName}::{exportName}"); diff --git a/Dalamud/Interface/Internal/Asserts/AssertHandler.cs b/Dalamud/Interface/Internal/Asserts/AssertHandler.cs index 56050cdfb..91323f8ac 100644 --- a/Dalamud/Interface/Internal/Asserts/AssertHandler.cs +++ b/Dalamud/Interface/Internal/Asserts/AssertHandler.cs @@ -29,14 +29,14 @@ internal class AssertHandler : IDisposable /// /// Initializes a new instance of the class. /// - public AssertHandler() + public unsafe AssertHandler() { - this.callback = (expr, file, line) => this.OnImGuiAssert(expr, file, line); + this.callback = this.OnImGuiAssert; } - private delegate void AssertCallbackDelegate( - [MarshalAs(UnmanagedType.LPStr)] string expr, - [MarshalAs(UnmanagedType.LPStr)] string file, + private unsafe delegate void AssertCallbackDelegate( + void* expr, + void* file, int line); /// @@ -53,15 +53,15 @@ internal class AssertHandler : IDisposable /// /// Register the cimgui assert handler with the native library. /// - public void Setup() + public unsafe void Setup() { - CustomNativeFunctions.igCustom_SetAssertCallback(this.callback); + CustomNativeFunctions.igCustom_SetAssertCallback(Marshal.GetFunctionPointerForDelegate(this.callback).ToPointer()); } /// /// Unregister the cimgui assert handler with the native library. /// - public void Shutdown() + public unsafe void Shutdown() { CustomNativeFunctions.igCustom_SetAssertCallback(null); } @@ -72,8 +72,19 @@ internal class AssertHandler : IDisposable this.Shutdown(); } - private void OnImGuiAssert(string expr, string file, int line) + private unsafe void OnImGuiAssert(void* pExpr, void* pFile, int line) { + var expr = Marshal.PtrToStringAnsi(new IntPtr(pExpr)); + var file = Marshal.PtrToStringAnsi(new IntPtr(pFile)); + if (expr == null || file == null) + { + Log.Warning("ImGui assertion failed: {Expr} at {File}:{Line} (failed to parse)", + expr, + file, + line); + return; + } + var key = $"{file}:{line}"; if (this.ignoredAsserts.Contains(key)) return; @@ -218,11 +229,11 @@ internal class AssertHandler : IDisposable } } - private static class CustomNativeFunctions + private static unsafe class CustomNativeFunctions { [DllImport("cimgui", CallingConvention = CallingConvention.Cdecl)] #pragma warning disable SA1300 - public static extern void igCustom_SetAssertCallback(AssertCallbackDelegate? callback); + public static extern void igCustom_SetAssertCallback(void* cb); #pragma warning restore SA1300 } } diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index d6624e54e..736f0072c 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -41,6 +41,8 @@ using TerraFX.Interop.Windows; using static TerraFX.Interop.Windows.Windows; +using DWMWINDOWATTRIBUTE = Windows.Win32.Graphics.Dwm.DWMWINDOWATTRIBUTE; + // general dev notes, here because it's easiest /* @@ -487,12 +489,13 @@ internal partial class InterfaceManager : IInternalDisposableService { if (this.GameWindowHandle == 0) throw new InvalidOperationException("Game window is not yet ready."); + var value = enabled ? 1u : 0u; - DwmSetWindowAttribute( - this.GameWindowHandle, - (uint)DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, + global::Windows.Win32.PInvoke.DwmSetWindowAttribute( + new(this.GameWindowHandle.Value), + DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, &value, - sizeof(int)).ThrowOnError(); + sizeof(uint)).ThrowOnFailure(); } private static InterfaceManager WhenFontsReady() diff --git a/Dalamud/Interface/ManagedFontAtlas/IFontAtlasBuildToolkitPreBuild.cs b/Dalamud/Interface/ManagedFontAtlas/IFontAtlasBuildToolkitPreBuild.cs index 16cb83979..80757d831 100644 --- a/Dalamud/Interface/ManagedFontAtlas/IFontAtlasBuildToolkitPreBuild.cs +++ b/Dalamud/Interface/ManagedFontAtlas/IFontAtlasBuildToolkitPreBuild.cs @@ -70,12 +70,12 @@ public interface IFontAtlasBuildToolkitPreBuild : IFontAtlasBuildToolkit /// Adds a font from memory region allocated using .
/// It WILL crash if you try to use a memory pointer allocated in some other way.
/// - /// Do NOT call on the once this function has + /// Do NOT call on the once this function has /// been called, unless is set and the function has thrown an error. /// ///
/// Memory address for the data allocated using . - /// The size of the font file.. + /// The size of the font file. /// The font config. /// Free if an exception happens. /// A debug tag. @@ -155,7 +155,7 @@ public interface IFontAtlasBuildToolkitPreBuild : IFontAtlasBuildToolkit /// used as the font size. Specify -1 to use the default font size. /// /// The glyph ranges. Use .ToGlyphRange to build. - /// A font returned from . + /// A font returned from . ImFontPtr AddDalamudDefaultFont(float sizePx, ushort[]? glyphRanges = null); /// diff --git a/Dalamud/Interface/ManagedFontAtlas/IFontHandle.cs b/Dalamud/Interface/ManagedFontAtlas/IFontHandle.cs index 0e5a445cc..a0b277c39 100644 --- a/Dalamud/Interface/ManagedFontAtlas/IFontHandle.cs +++ b/Dalamud/Interface/ManagedFontAtlas/IFontHandle.cs @@ -56,9 +56,9 @@ public interface IFontHandle : IDisposable /// You may not access the font once you dispose this object. /// /// A disposable object that will pop the font on dispose. - /// If called outside of the main thread. + /// If called outside the main thread. /// - /// This function uses , and may do extra things. + /// This function uses , and may do extra things. /// Use or to undo this operation. /// Do not use . /// diff --git a/Dalamud/Memory/MemoryHelper.cs b/Dalamud/Memory/MemoryHelper.cs index aeee84f7a..0f557c3f6 100644 --- a/Dalamud/Memory/MemoryHelper.cs +++ b/Dalamud/Memory/MemoryHelper.cs @@ -9,10 +9,9 @@ using Dalamud.Utility; using FFXIVClientStructs.FFXIV.Client.System.Memory; using FFXIVClientStructs.FFXIV.Client.System.String; using Microsoft.Extensions.ObjectPool; +using Windows.Win32.Foundation; using Windows.Win32.System.Memory; -using static Dalamud.NativeFunctions; - using LPayloadType = Lumina.Text.Payloads.PayloadType; using LSeString = Lumina.Text.SeString; @@ -28,6 +27,8 @@ public static unsafe class MemoryHelper private static readonly ObjectPool StringBuilderPool = ObjectPool.Create(new StringBuilderPooledObjectPolicy()); + private static readonly HANDLE ThisProcessPseudoHandle = new(unchecked((nint)0xFFFFFFFF)); + #region Cast /// Casts the given memory address as the reference to the live object. @@ -863,14 +864,22 @@ public static unsafe class MemoryHelper unchecked { var length = value.Length; - var result = NativeFunctions.ReadProcessMemory((nint)0xFFFFFFFF, memoryAddress, value, length, out _); + fixed (byte* pVal = value) + { + var result = Windows.Win32.PInvoke.ReadProcessMemory( + ThisProcessPseudoHandle, + memoryAddress.ToPointer(), + pVal, + (nuint)length, + null); - if (!result) - throw new MemoryReadException($"Unable to read memory at {Util.DescribeAddress(memoryAddress)} of length {length} (result={result})"); + if (!result) + throw new MemoryReadException($"Unable to read memory at {Util.DescribeAddress(memoryAddress)} of length {length} (result={result})"); - var last = Marshal.GetLastWin32Error(); - if (last > 0) - throw new MemoryReadException($"Unable to read memory at {Util.DescribeAddress(memoryAddress)} of length {length} (error={last})"); + var last = Marshal.GetLastWin32Error(); + if (last > 0) + throw new MemoryReadException($"Unable to read memory at {Util.DescribeAddress(memoryAddress)} of length {length} (error={last})"); + } } } @@ -885,14 +894,22 @@ public static unsafe class MemoryHelper unchecked { var length = data.Length; - var result = NativeFunctions.WriteProcessMemory((nint)0xFFFFFFFF, memoryAddress, data, length, out _); + fixed (byte* pData = data) + { + var result = Windows.Win32.PInvoke.WriteProcessMemory( + ThisProcessPseudoHandle, + memoryAddress.ToPointer(), + pData, + (nuint)length, + null); - if (!result) - throw new MemoryWriteException($"Unable to write memory at {Util.DescribeAddress(memoryAddress)} of length {length} (result={result})"); + if (!result) + throw new MemoryWriteException($"Unable to write memory at {Util.DescribeAddress(memoryAddress)} of length {length} (result={result})"); - var last = Marshal.GetLastWin32Error(); - if (last > 0) - throw new MemoryWriteException($"Unable to write memory at {Util.DescribeAddress(memoryAddress)} of length {length} (error={last})"); + var last = Marshal.GetLastWin32Error(); + if (last > 0) + throw new MemoryWriteException($"Unable to write memory at {Util.DescribeAddress(memoryAddress)} of length {length} (error={last})"); + } } } diff --git a/Dalamud/NativeFunctions.cs b/Dalamud/NativeFunctions.cs deleted file mode 100644 index fd45ffba9..000000000 --- a/Dalamud/NativeFunctions.cs +++ /dev/null @@ -1,1660 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Text; - -namespace Dalamud; - -/// -/// Native user32 functions. -/// -internal static partial class NativeFunctions -{ - /// - /// FLASHW_* from winuser. - /// - public enum FlashWindow : uint - { - /// - /// Stop flashing. The system restores the window to its original state. - /// - Stop = 0, - - /// - /// Flash the window caption. - /// - Caption = 1, - - /// - /// Flash the taskbar button. - /// - Tray = 2, - - /// - /// Flash both the window caption and taskbar button. - /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. - /// - All = 3, - - /// - /// Flash continuously, until the FLASHW_STOP flag is set. - /// - Timer = 4, - - /// - /// Flash continuously until the window comes to the foreground. - /// - TimerNoFG = 12, - } - - /// - /// IDC_* from winuser. - /// - public enum CursorType - { - /// - /// Standard arrow and small hourglass. - /// - AppStarting = 32650, - - /// - /// Standard arrow. - /// - Arrow = 32512, - - /// - /// Crosshair. - /// - Cross = 32515, - - /// - /// Hand. - /// - Hand = 32649, - - /// - /// Arrow and question mark. - /// - Help = 32651, - - /// - /// I-beam. - /// - IBeam = 32513, - - /// - /// Obsolete for applications marked version 4.0 or later. - /// - Icon = 32641, - - /// - /// Slashed circle. - /// - No = 32648, - - /// - /// Obsolete for applications marked version 4.0 or later.Use IDC_SIZEALL. - /// - Size = 32640, - - /// - /// Four-pointed arrow pointing north, south, east, and west. - /// - SizeAll = 32646, - - /// - /// Double-pointed arrow pointing northeast and southwest. - /// - SizeNeSw = 32643, - - /// - /// Double-pointed arrow pointing north and south. - /// - SizeNS = 32645, - - /// - /// Double-pointed arrow pointing northwest and southeast. - /// - SizeNwSe = 32642, - - /// - /// Double-pointed arrow pointing west and east. - /// - SizeWE = 32644, - - /// - /// Vertical arrow. - /// - UpArrow = 32516, - - /// - /// Hourglass. - /// - Wait = 32514, - } - - /// - /// GWL_* from winuser. - /// - public enum WindowLongType - { - /// - /// Sets a new extended window style. - /// - ExStyle = -20, - - /// - /// Sets a new application instance handle. - /// - HInstance = -6, - - /// - /// Sets a new identifier of the child window.The window cannot be a top-level window. - /// - Id = -12, - - /// - /// Sets a new window style. - /// - Style = -16, - - /// - /// Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero. - /// - UserData = -21, - - /// - /// Sets a new address for the window procedure. - /// - WndProc = -4, - - // The following values are also available when the hWnd parameter identifies a dialog box. - - // /// - // /// Sets the new pointer to the dialog box procedure. - // /// - // DWLP_DLGPROC = DWLP_MSGRESULT + sizeof(LRESULT), - - /// - /// Sets the return value of a message processed in the dialog box procedure. - /// - MsgResult = 0, - - // /// - // /// Sets new extra information that is private to the application, such as handles or pointers. - // /// - // DWLP_USER = DWLP_DLGPROC + sizeof(DLGPROC), - } - - /// - /// WM_* from winuser. - /// These are spread throughout multiple files, find the documentation manually if you need it. - /// https://gist.github.com/amgine/2395987. - /// - [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:Enumeration items should be documented", Justification = "No documentation available.")] - public enum WindowsMessage - { - WM_NULL = 0x0000, - WM_CREATE = 0x0001, - WM_DESTROY = 0x0002, - WM_MOVE = 0x0003, - WM_SIZE = 0x0005, - WM_ACTIVATE = 0x0006, - WM_SETFOCUS = 0x0007, - WM_KILLFOCUS = 0x0008, - WM_ENABLE = 0x000A, - WM_SETREDRAW = 0x000B, - WM_SETTEXT = 0x000C, - WM_GETTEXT = 0x000D, - WM_GETTEXTLENGTH = 0x000E, - WM_PAINT = 0x000F, - WM_CLOSE = 0x0010, - WM_QUERYENDSESSION = 0x0011, - WM_QUERYOPEN = 0x0013, - WM_ENDSESSION = 0x0016, - WM_QUIT = 0x0012, - WM_ERASEBKGND = 0x0014, - WM_SYSCOLORCHANGE = 0x0015, - WM_SHOWWINDOW = 0x0018, - WM_WININICHANGE = 0x001A, - WM_SETTINGCHANGE = WM_WININICHANGE, - WM_DEVMODECHANGE = 0x001B, - WM_ACTIVATEAPP = 0x001C, - WM_FONTCHANGE = 0x001D, - WM_TIMECHANGE = 0x001E, - WM_CANCELMODE = 0x001F, - WM_SETCURSOR = 0x0020, - WM_MOUSEACTIVATE = 0x0021, - WM_CHILDACTIVATE = 0x0022, - WM_QUEUESYNC = 0x0023, - WM_GETMINMAXINFO = 0x0024, - WM_PAINTICON = 0x0026, - WM_ICONERASEBKGND = 0x0027, - WM_NEXTDLGCTL = 0x0028, - WM_SPOOLERSTATUS = 0x002A, - WM_DRAWITEM = 0x002B, - WM_MEASUREITEM = 0x002C, - WM_DELETEITEM = 0x002D, - WM_VKEYTOITEM = 0x002E, - WM_CHARTOITEM = 0x002F, - WM_SETFONT = 0x0030, - WM_GETFONT = 0x0031, - WM_SETHOTKEY = 0x0032, - WM_GETHOTKEY = 0x0033, - WM_QUERYDRAGICON = 0x0037, - WM_COMPAREITEM = 0x0039, - WM_GETOBJECT = 0x003D, - WM_COMPACTING = 0x0041, - WM_COMMNOTIFY = 0x0044, - WM_WINDOWPOSCHANGING = 0x0046, - WM_WINDOWPOSCHANGED = 0x0047, - WM_POWER = 0x0048, - WM_COPYDATA = 0x004A, - WM_CANCELJOURNAL = 0x004B, - WM_NOTIFY = 0x004E, - WM_INPUTLANGCHANGEREQUEST = 0x0050, - WM_INPUTLANGCHANGE = 0x0051, - WM_TCARD = 0x0052, - WM_HELP = 0x0053, - WM_USERCHANGED = 0x0054, - WM_NOTIFYFORMAT = 0x0055, - WM_CONTEXTMENU = 0x007B, - WM_STYLECHANGING = 0x007C, - WM_STYLECHANGED = 0x007D, - WM_DISPLAYCHANGE = 0x007E, - WM_GETICON = 0x007F, - WM_SETICON = 0x0080, - WM_NCCREATE = 0x0081, - WM_NCDESTROY = 0x0082, - WM_NCCALCSIZE = 0x0083, - WM_NCHITTEST = 0x0084, - WM_NCPAINT = 0x0085, - WM_NCACTIVATE = 0x0086, - WM_GETDLGCODE = 0x0087, - WM_SYNCPAINT = 0x0088, - - WM_NCMOUSEMOVE = 0x00A0, - WM_NCLBUTTONDOWN = 0x00A1, - WM_NCLBUTTONUP = 0x00A2, - WM_NCLBUTTONDBLCLK = 0x00A3, - WM_NCRBUTTONDOWN = 0x00A4, - WM_NCRBUTTONUP = 0x00A5, - WM_NCRBUTTONDBLCLK = 0x00A6, - WM_NCMBUTTONDOWN = 0x00A7, - WM_NCMBUTTONUP = 0x00A8, - WM_NCMBUTTONDBLCLK = 0x00A9, - WM_NCXBUTTONDOWN = 0x00AB, - WM_NCXBUTTONUP = 0x00AC, - WM_NCXBUTTONDBLCLK = 0x00AD, - - WM_INPUT_DEVICE_CHANGE = 0x00FE, - WM_INPUT = 0x00FF, - - WM_KEYFIRST = 0x0100, - WM_KEYDOWN = WM_KEYFIRST, - WM_KEYUP = 0x0101, - WM_CHAR = 0x0102, - WM_DEADCHAR = 0x0103, - WM_SYSKEYDOWN = 0x0104, - WM_SYSKEYUP = 0x0105, - WM_SYSCHAR = 0x0106, - WM_SYSDEADCHAR = 0x0107, - WM_UNICHAR = 0x0109, - WM_KEYLAST = WM_UNICHAR, - - WM_IME_STARTCOMPOSITION = 0x010D, - WM_IME_ENDCOMPOSITION = 0x010E, - WM_IME_COMPOSITION = 0x010F, - WM_IME_KEYLAST = WM_IME_COMPOSITION, - - WM_INITDIALOG = 0x0110, - WM_COMMAND = 0x0111, - WM_SYSCOMMAND = 0x0112, - WM_TIMER = 0x0113, - WM_HSCROLL = 0x0114, - WM_VSCROLL = 0x0115, - WM_INITMENU = 0x0116, - WM_INITMENUPOPUP = 0x0117, - WM_MENUSELECT = 0x011F, - WM_MENUCHAR = 0x0120, - WM_ENTERIDLE = 0x0121, - WM_MENURBUTTONUP = 0x0122, - WM_MENUDRAG = 0x0123, - WM_MENUGETOBJECT = 0x0124, - WM_UNINITMENUPOPUP = 0x0125, - WM_MENUCOMMAND = 0x0126, - - WM_CHANGEUISTATE = 0x0127, - WM_UPDATEUISTATE = 0x0128, - WM_QUERYUISTATE = 0x0129, - - WM_CTLCOLORMSGBOX = 0x0132, - WM_CTLCOLOREDIT = 0x0133, - WM_CTLCOLORLISTBOX = 0x0134, - WM_CTLCOLORBTN = 0x0135, - WM_CTLCOLORDLG = 0x0136, - WM_CTLCOLORSCROLLBAR = 0x0137, - WM_CTLCOLORSTATIC = 0x0138, - MN_GETHMENU = 0x01E1, - - WM_MOUSEFIRST = 0x0200, - WM_MOUSEMOVE = WM_MOUSEFIRST, - WM_LBUTTONDOWN = 0x0201, - WM_LBUTTONUP = 0x0202, - WM_LBUTTONDBLCLK = 0x0203, - WM_RBUTTONDOWN = 0x0204, - WM_RBUTTONUP = 0x0205, - WM_RBUTTONDBLCLK = 0x0206, - WM_MBUTTONDOWN = 0x0207, - WM_MBUTTONUP = 0x0208, - WM_MBUTTONDBLCLK = 0x0209, - WM_MOUSEWHEEL = 0x020A, - WM_XBUTTONDOWN = 0x020B, - WM_XBUTTONUP = 0x020C, - WM_XBUTTONDBLCLK = 0x020D, - WM_MOUSEHWHEEL = 0x020E, - - WM_PARENTNOTIFY = 0x0210, - WM_ENTERMENULOOP = 0x0211, - WM_EXITMENULOOP = 0x0212, - - WM_NEXTMENU = 0x0213, - WM_SIZING = 0x0214, - WM_CAPTURECHANGED = 0x0215, - WM_MOVING = 0x0216, - - WM_POWERBROADCAST = 0x0218, - - WM_DEVICECHANGE = 0x0219, - - WM_MDICREATE = 0x0220, - WM_MDIDESTROY = 0x0221, - WM_MDIACTIVATE = 0x0222, - WM_MDIRESTORE = 0x0223, - WM_MDINEXT = 0x0224, - WM_MDIMAXIMIZE = 0x0225, - WM_MDITILE = 0x0226, - WM_MDICASCADE = 0x0227, - WM_MDIICONARRANGE = 0x0228, - WM_MDIGETACTIVE = 0x0229, - - WM_MDISETMENU = 0x0230, - WM_ENTERSIZEMOVE = 0x0231, - WM_EXITSIZEMOVE = 0x0232, - WM_DROPFILES = 0x0233, - WM_MDIREFRESHMENU = 0x0234, - - WM_IME_SETCONTEXT = 0x0281, - WM_IME_NOTIFY = 0x0282, - WM_IME_CONTROL = 0x0283, - WM_IME_COMPOSITIONFULL = 0x0284, - WM_IME_SELECT = 0x0285, - WM_IME_CHAR = 0x0286, - WM_IME_REQUEST = 0x0288, - WM_IME_KEYDOWN = 0x0290, - WM_IME_KEYUP = 0x0291, - - WM_MOUSEHOVER = 0x02A1, - WM_MOUSELEAVE = 0x02A3, - WM_NCMOUSEHOVER = 0x02A0, - WM_NCMOUSELEAVE = 0x02A2, - - WM_WTSSESSION_CHANGE = 0x02B1, - - WM_TABLET_FIRST = 0x02c0, - WM_TABLET_LAST = 0x02df, - - WM_CUT = 0x0300, - WM_COPY = 0x0301, - WM_PASTE = 0x0302, - WM_CLEAR = 0x0303, - WM_UNDO = 0x0304, - WM_RENDERFORMAT = 0x0305, - WM_RENDERALLFORMATS = 0x0306, - WM_DESTROYCLIPBOARD = 0x0307, - WM_DRAWCLIPBOARD = 0x0308, - WM_PAINTCLIPBOARD = 0x0309, - WM_VSCROLLCLIPBOARD = 0x030A, - WM_SIZECLIPBOARD = 0x030B, - WM_ASKCBFORMATNAME = 0x030C, - WM_CHANGECBCHAIN = 0x030D, - WM_HSCROLLCLIPBOARD = 0x030E, - WM_QUERYNEWPALETTE = 0x030F, - WM_PALETTEISCHANGING = 0x0310, - WM_PALETTECHANGED = 0x0311, - WM_HOTKEY = 0x0312, - - WM_PRINT = 0x0317, - WM_PRINTCLIENT = 0x0318, - - WM_APPCOMMAND = 0x0319, - - WM_THEMECHANGED = 0x031A, - - WM_CLIPBOARDUPDATE = 0x031D, - - WM_DWMCOMPOSITIONCHANGED = 0x031E, - WM_DWMNCRENDERINGCHANGED = 0x031F, - WM_DWMCOLORIZATIONCOLORCHANGED = 0x0320, - WM_DWMWINDOWMAXIMIZEDCHANGE = 0x0321, - - WM_GETTITLEBARINFOEX = 0x033F, - - WM_HANDHELDFIRST = 0x0358, - WM_HANDHELDLAST = 0x035F, - - WM_AFXFIRST = 0x0360, - WM_AFXLAST = 0x037F, - - WM_PENWINFIRST = 0x0380, - WM_PENWINLAST = 0x038F, - - WM_APP = 0x8000, - - WM_USER = 0x0400, - - WM_REFLECT = WM_USER + 0x1C00, - } - - /// - /// Returns true if the current application has focus, false otherwise. - /// - /// - /// If the current application is focused. - /// - public static bool ApplicationIsActivated() - { - var activatedHandle = GetForegroundWindow(); - if (activatedHandle == IntPtr.Zero) - return false; // No window is currently activated - - _ = GetWindowThreadProcessId(activatedHandle, out var activeProcId); - if (Marshal.GetLastWin32Error() != 0) - return false; - - return activeProcId == Environment.ProcessId; - } - - /// - /// Passes message information to the specified window procedure. - /// - /// - /// The previous window procedure. If this value is obtained by calling the GetWindowLong function with the nIndex parameter set to - /// GWL_WNDPROC or DWL_DLGPROC, it is actually either the address of a window or dialog box procedure, or a special internal value - /// meaningful only to CallWindowProc. - /// - /// - /// A handle to the window procedure to receive the message. - /// - /// - /// The message. - /// - /// - /// Additional message-specific information. The contents of this parameter depend on the value of the Msg parameter. - /// - /// - /// More additional message-specific information. The contents of this parameter depend on the value of the Msg parameter. - /// - /// - /// Use the CallWindowProc function for window subclassing. Usually, all windows with the same class share one window procedure. A - /// subclass is a window or set of windows with the same class whose messages are intercepted and processed by another window procedure - /// (or procedures) before being passed to the window procedure of the class. - /// The SetWindowLong function creates the subclass by changing the window procedure associated with a particular window, causing the - /// system to call the new window procedure instead of the previous one.An application must pass any messages not processed by the new - /// window procedure to the previous window procedure by calling CallWindowProc.This allows the application to create a chain of window - /// procedures. - /// - [DllImport("user32.dll")] - public static extern long CallWindowProcW(IntPtr lpPrevWndFunc, IntPtr hWnd, uint msg, ulong wParam, long lParam); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-flashwindowex. - /// Flashes the specified window. It does not change the active state of the window. - /// - /// - /// A pointer to a FLASHWINFO structure. - /// - /// - /// The return value specifies the window's state before the call to the FlashWindowEx function. If the window caption - /// was drawn as active before the call, the return value is nonzero. Otherwise, the return value is zero. - /// - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool FlashWindowEx(ref FlashWindowInfo pwfi); - - /// - /// Retrieves a handle to the foreground window (the window with which the user is currently working). The system assigns - /// a slightly higher priority to the thread that creates the foreground window than it does to other threads. - /// - /// - /// The return value is a handle to the foreground window. The foreground window can be NULL in certain circumstances, - /// such as when a window is losing activation. - /// - [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] - public static extern IntPtr GetForegroundWindow(); - - /// - /// Retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the - /// process that created the window. - /// - /// - /// A handle to the window. - /// - /// - /// A pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId - /// copies the identifier of the process to the variable; otherwise, it does not. - /// - /// - /// The return value is the identifier of the thread that created the window. - /// - [DllImport("user32.dll", CharSet = CharSet.Auto)] - public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId); - - /// - /// Changes an attribute of the specified window. The function also sets a value at the specified offset in the extra window memory. - /// - /// - /// A handle to the window and, indirectly, the class to which the window belongs. The SetWindowLongPtr function fails if the - /// process that owns the window specified by the hWnd parameter is at a higher process privilege in the UIPI hierarchy than the - /// process the calling thread resides in. - /// - /// - /// The zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window - /// memory, minus the size of a LONG_PTR. To set any other value, specify one of the values. - /// - /// - /// The replacement value. - /// - /// - /// If the function succeeds, the return value is the previous value of the specified offset. If the function fails, the return - /// value is zero.To get extended error information, call GetLastError. If the previous value is zero and the function succeeds, - /// the return value is zero, but the function does not clear the last error information. To determine success or failure, clear - /// the last error information by calling SetLastError with 0, then call SetWindowLongPtr.Function failure will be indicated by - /// a return value of zero and a GetLastError result that is nonzero. - /// - [DllImport("user32.dll")] - public static extern IntPtr SetWindowLongPtrW(IntPtr hWnd, WindowLongType nIndex, IntPtr dwNewLong); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-flashwinfo. - /// Contains the flash status for a window and the number of times the system should flash the window. - /// - [StructLayout(LayoutKind.Sequential)] - public struct FlashWindowInfo - { - /// - /// The size of the structure, in bytes. - /// - public uint Size; - - /// - /// A handle to the window to be flashed. The window can be either opened or minimized. - /// - public IntPtr Hwnd; - - /// - /// The flash status. This parameter can be one or more of the FlashWindow enum values. - /// - public FlashWindow Flags; - - /// - /// The number of times to flash the window. - /// - public uint Count; - - /// - /// The rate at which the window is to be flashed, in milliseconds. If dwTimeout is zero, the function uses the - /// default cursor blink rate. - /// - public uint Timeout; - } - - /// - /// Parameters for use with SystemParametersInfo. - /// - public enum AccessibilityParameter - { -#pragma warning disable SA1602 - SPI_GETCLIENTAREAANIMATION = 0x1042, -#pragma warning restore SA1602 - } - - /// - /// Retrieves or sets the value of one of the system-wide parameters. This function can also update the user profile while setting a parameter. - /// - /// The system-wide parameter to be retrieved or set. - /// A parameter whose usage and format depends on the system parameter being queried or set. - /// A parameter whose usage and format depends on the system parameter being queried or set. If not otherwise indicated, you must specify zero for this parameter. - /// If a system parameter is being set, specifies whether the user profile is to be updated, and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to notify them of the change. - /// If the function succeeds, the return value is a nonzero value. - [DllImport("user32.dll")] - public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni); -} - -/// -/// Native imm32 functions. -/// -internal static partial class NativeFunctions -{ - /// - /// GCS_* from imm32. - /// These values are used with ImmGetCompositionString and WM_IME_COMPOSITION. - /// - [Flags] - public enum IMEComposition - { - /// - /// Retrieve or update the attribute of the composition string. - /// - CompAttr = 0x0010, - - /// - /// Retrieve or update clause information of the composition string. - /// - CompClause = 0x0020, - - /// - /// Retrieve or update the attributes of the reading string of the current composition. - /// - CompReadAttr = 0x0002, - - /// - /// Retrieve or update the clause information of the reading string of the composition string. - /// - CompReadClause = 0x0004, - - /// - /// Retrieve or update the reading string of the current composition. - /// - CompReadStr = 0x0001, - - /// - /// Retrieve or update the current composition string. - /// - CompStr = 0x0008, - - /// - /// Retrieve or update the cursor position in composition string. - /// - CursorPos = 0x0080, - - /// - /// Retrieve or update the starting position of any changes in composition string. - /// - DeltaStart = 0x0100, - - /// - /// Retrieve or update clause information of the result string. - /// - ResultClause = 0x1000, - - /// - /// Retrieve or update clause information of the reading string. - /// - ResultReadClause = 0x0400, - - /// - /// Retrieve or update the reading string. - /// - ResultReadStr = 0x0200, - - /// - /// Retrieve or update the string of the composition result. - /// - ResultStr = 0x0800, - } - - /// - /// IMN_* from imm32. - /// Input Method Manager Commands, this enum is not exhaustive. - /// - public enum IMECommand - { - /// - /// Notifies the application when an IME is about to change the content of the candidate window. - /// - ChangeCandidate = 0x0003, - - /// - /// Notifies an application when an IME is about to close the candidates window. - /// - CloseCandidate = 0x0004, - - /// - /// Notifies an application when an IME is about to open the candidate window. - /// - OpenCandidate = 0x0005, - - /// - /// Notifies an application when the conversion mode of the input context is updated. - /// - SetConversionMode = 0x0006, - } - - /// - /// Returns the input context associated with the specified window. - /// - /// Unnamed parameter 1. - /// - /// Returns the handle to the input context. - /// - [DllImport("imm32.dll")] - public static extern IntPtr ImmGetContext(IntPtr hWnd); - - /// - /// Retrieves information about the composition string. - /// - /// - /// Unnamed parameter 1. - /// - /// - /// Unnamed parameter 2. - /// - /// - /// Pointer to a buffer in which the function retrieves the composition string information. - /// - /// - /// Size, in bytes, of the output buffer, even if the output is a Unicode string. The application sets this parameter to 0 - /// if the function is to return the size of the required output buffer. - /// - /// - /// Returns the number of bytes copied to the output buffer. If dwBufLen is set to 0, the function returns the buffer size, - /// in bytes, required to receive all requested information, excluding the terminating null character. The return value is - /// always the size, in bytes, even if the requested data is a Unicode string. - /// This function returns one of the following negative error codes if it does not succeed: - /// - IMM_ERROR_NODATA.Composition data is not ready in the input context. - /// - IMM_ERROR_GENERAL.General error detected by IME. - /// - [DllImport("imm32.dll")] - public static extern long ImmGetCompositionStringW(IntPtr hImc, IMEComposition arg2, IntPtr lpBuf, uint dwBufLen); - - /// - /// Retrieves a candidate list. - /// - /// - /// Unnamed parameter 1. - /// - /// - /// Zero-based index of the candidate list. - /// - /// - /// Pointer to a CANDIDATELIST structure in which the function retrieves the candidate list. - /// - /// - /// Size, in bytes, of the buffer to receive the candidate list. The application can specify 0 for this parameter if the - /// function is to return the required size of the output buffer only. - /// - /// - /// Returns the number of bytes copied to the candidate list buffer if successful. If the application has supplied 0 for - /// the dwBufLen parameter, the function returns the size required for the candidate list buffer. The function returns 0 - /// if it does not succeed. - /// - [DllImport("imm32.dll")] - public static extern long ImmGetCandidateListW(IntPtr hImc, uint deIndex, IntPtr lpCandList, uint dwBufLen); - - /// - /// Sets the position of the composition window. - /// - /// - /// Unnamed parameter 1. - /// - /// - /// Pointer to a COMPOSITIONFORM structure that contains the new position and other related information about - /// the composition window. - /// - /// - /// Returns a nonzero value if successful, or 0 otherwise. - /// - [DllImport("imm32.dll", CharSet = CharSet.Auto)] - public static extern bool ImmSetCompositionWindow(IntPtr hImc, ref CompositionForm frm); - - /// - /// Releases the input context and unlocks the memory associated in the input context. An application must call this - /// function for each call to the ImmGetContext function. - /// - /// - /// Unnamed parameter 1. - /// - /// - /// Unnamed parameter 2. - /// - /// - /// Returns a nonzero value if successful, or 0 otherwise. - /// - [DllImport("imm32.dll", CharSet = CharSet.Auto)] - public static extern bool ImmReleaseContext(IntPtr hwnd, IntPtr hImc); - - /// - /// Contains information about a candidate list. - /// - public struct CandidateList - { - /// - /// Size, in bytes, of the structure, the offset array, and all candidate strings. - /// - public int Size; - - /// - /// Candidate style values. This member can have one or more of the IME_CAND_* values. - /// - public int Style; - - /// - /// Number of candidate strings. - /// - public int Count; - - /// - /// Index of the selected candidate string. - /// - public int Selection; - - /// - /// Index of the first candidate string in the candidate window. This varies as the user presses the PAGE UP and PAGE DOWN keys. - /// - public int PageStart; - - /// - /// Number of candidate strings to be shown in one page in the candidate window. The user can move to the next page by pressing IME-defined keys, such as the PAGE UP or PAGE DOWN key. If this number is 0, an application can define a proper value by itself. - /// - public int PageSize; - - // /// - // /// Offset to the start of the first candidate string, relative to the start of this structure. The offsets - // /// for subsequent strings immediately follow this member, forming an array of 32-bit offsets. - // /// - // public IntPtr Offset; // manually handle - } - - /// - /// Contains style and position information for a composition window. - /// - [StructLayout(LayoutKind.Sequential)] - public struct CompositionForm - { - /// - /// Position style. This member can be one of the CFS_* values. - /// - public int Style; - - /// - /// A POINT structure containing the coordinates of the upper left corner of the composition window. - /// - public Point CurrentPos; - - /// - /// A RECT structure containing the coordinates of the upper left and lower right corners of the composition window. - /// - public Rect Area; - } - - /// - /// Contains coordinates for a point. - /// - [StructLayout(LayoutKind.Sequential)] - public struct Point - { - /// - /// The X position. - /// - public int X; - - /// - /// The Y position. - /// - public int Y; - } - - /// - /// Contains dimensions for a rectangle. - /// - [StructLayout(LayoutKind.Sequential)] - public struct Rect - { - /// - /// The left position. - /// - public int Left; - - /// - /// The top position. - /// - public int Top; - - /// - /// The right position. - /// - public int Right; - - /// - /// The bottom position. - /// - public int Bottom; - } -} - -/// -/// Native kernel32 functions. -/// -[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1629:Documentation text should end with a period", Justification = "Stupid rule")] -internal static partial class NativeFunctions -{ - /// - /// SEM_* from errhandlingapi. - /// - [Flags] - public enum ErrorModes : uint - { - /// - /// Use the system default, which is to display all error dialog boxes. - /// - SystemDefault = 0x0, - - /// - /// The system does not display the critical-error-handler message box. Instead, the system sends the error to the - /// calling process. Best practice is that all applications call the process-wide SetErrorMode function with a parameter - /// of SEM_FAILCRITICALERRORS at startup. This is to prevent error mode dialogs from hanging the application. - /// - FailCriticalErrors = 0x0001, - - /// - /// The system automatically fixes memory alignment faults and makes them invisible to the application. It does - /// this for the calling process and any descendant processes. This feature is only supported by certain processor - /// architectures. For more information, see the Remarks section. After this value is set for a process, subsequent - /// attempts to clear the value are ignored. - /// - NoAlignmentFaultExcept = 0x0004, - - /// - /// The system does not display the Windows Error Reporting dialog. - /// - NoGpFaultErrorBox = 0x0002, - - /// - /// The OpenFile function does not display a message box when it fails to find a file. Instead, the error is returned - /// to the caller. This error mode overrides the OF_PROMPT flag. - /// - NoOpenFileErrorBox = 0x8000, - } - - /// - /// PAGE_* from memoryapi. - /// - [Flags] - public enum MemoryProtection - { - /// - /// Enables execute access to the committed region of pages. An attempt to write to the committed region results - /// in an access violation. This flag is not supported by the CreateFileMapping function. - /// - Execute = 0x10, - - /// - /// Enables execute or read-only access to the committed region of pages. An attempt to write to the committed region - /// results in an access violation. - /// - ExecuteRead = 0x20, - - /// - /// Enables execute, read-only, or read/write access to the committed region of pages. - /// - ExecuteReadWrite = 0x40, - - /// - /// Enables execute, read-only, or copy-on-write access to a mapped view of a file mapping object. An attempt to - /// write to a committed copy-on-write page results in a private copy of the page being made for the process. The - /// private page is marked as PAGE_EXECUTE_READWRITE, and the change is written to the new page. This flag is not - /// supported by the VirtualAlloc or VirtualAllocEx functions. - /// - ExecuteWriteCopy = 0x80, - - /// - /// Disables all access to the committed region of pages. An attempt to read from, write to, or execute the committed - /// region results in an access violation. This flag is not supported by the CreateFileMapping function. - /// - NoAccess = 0x01, - - /// - /// Enables read-only access to the committed region of pages. An attempt to write to the committed region results - /// in an access violation. If Data Execution Prevention is enabled, an attempt to execute code in the committed - /// region results in an access violation. - /// - ReadOnly = 0x02, - - /// - /// Enables read-only or read/write access to the committed region of pages. If Data Execution Prevention is enabled, - /// attempting to execute code in the committed region results in an access violation. - /// - ReadWrite = 0x04, - - /// - /// Enables read-only or copy-on-write access to a mapped view of a file mapping object. An attempt to write to - /// a committed copy-on-write page results in a private copy of the page being made for the process. The private - /// page is marked as PAGE_READWRITE, and the change is written to the new page. If Data Execution Prevention is - /// enabled, attempting to execute code in the committed region results in an access violation. This flag is not - /// supported by the VirtualAlloc or VirtualAllocEx functions. - /// - WriteCopy = 0x08, - - /// - /// Sets all locations in the pages as invalid targets for CFG. Used along with any execute page protection like - /// PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE and PAGE_EXECUTE_WRITECOPY. Any indirect call to locations - /// in those pages will fail CFG checks and the process will be terminated. The default behavior for executable - /// pages allocated is to be marked valid call targets for CFG. This flag is not supported by the VirtualProtect - /// or CreateFileMapping functions. - /// - TargetsInvalid = 0x40000000, - - /// - /// Pages in the region will not have their CFG information updated while the protection changes for VirtualProtect. - /// For example, if the pages in the region was allocated using PAGE_TARGETS_INVALID, then the invalid information - /// will be maintained while the page protection changes. This flag is only valid when the protection changes to - /// an executable type like PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE and PAGE_EXECUTE_WRITECOPY. - /// The default behavior for VirtualProtect protection change to executable is to mark all locations as valid call - /// targets for CFG. - /// - TargetsNoUpdate = TargetsInvalid, - - /// - /// Pages in the region become guard pages. Any attempt to access a guard page causes the system to raise a - /// STATUS_GUARD_PAGE_VIOLATION exception and turn off the guard page status. Guard pages thus act as a one-time - /// access alarm. For more information, see Creating Guard Pages. When an access attempt leads the system to turn - /// off guard page status, the underlying page protection takes over. If a guard page exception occurs during a - /// system service, the service typically returns a failure status indicator. This value cannot be used with - /// PAGE_NOACCESS. This flag is not supported by the CreateFileMapping function. - /// - Guard = 0x100, - - /// - /// Sets all pages to be non-cachable. Applications should not use this attribute except when explicitly required - /// for a device. Using the interlocked functions with memory that is mapped with SEC_NOCACHE can result in an - /// EXCEPTION_ILLEGAL_INSTRUCTION exception. The PAGE_NOCACHE flag cannot be used with the PAGE_GUARD, PAGE_NOACCESS, - /// or PAGE_WRITECOMBINE flags. The PAGE_NOCACHE flag can be used only when allocating private memory with the - /// VirtualAlloc, VirtualAllocEx, or VirtualAllocExNuma functions. To enable non-cached memory access for shared - /// memory, specify the SEC_NOCACHE flag when calling the CreateFileMapping function. - /// - NoCache = 0x200, - - /// - /// Sets all pages to be write-combined. Applications should not use this attribute except when explicitly required - /// for a device. Using the interlocked functions with memory that is mapped as write-combined can result in an - /// EXCEPTION_ILLEGAL_INSTRUCTION exception. The PAGE_WRITECOMBINE flag cannot be specified with the PAGE_NOACCESS, - /// PAGE_GUARD, and PAGE_NOCACHE flags. The PAGE_WRITECOMBINE flag can be used only when allocating private memory - /// with the VirtualAlloc, VirtualAllocEx, or VirtualAllocExNuma functions. To enable write-combined memory access - /// for shared memory, specify the SEC_WRITECOMBINE flag when calling the CreateFileMapping function. - /// - WriteCombine = 0x400, - } - - /// - /// HEAP_* from heapapi. - /// - [Flags] - public enum HeapOptions - { - /// - /// Serialized access is not used when the heap functions access this heap. This option applies to all - /// subsequent heap function calls. Alternatively, you can specify this option on individual heap function - /// calls. The low-fragmentation heap (LFH) cannot be enabled for a heap created with this option. A heap - /// created with this option cannot be locked. - /// - NoSerialize = 0x00000001, - - /// - /// The system raises an exception to indicate failure (for example, an out-of-memory condition) for calls to - /// HeapAlloc and HeapReAlloc instead of returning NULL. - /// - GenerateExceptions = 0x00000004, - - /// - /// The allocated memory will be initialized to zero. Otherwise, the memory is not initialized to zero. - /// - ZeroMemory = 0x00000008, - - /// - /// All memory blocks that are allocated from this heap allow code execution, if the hardware enforces data - /// execution prevention. Use this flag heap in applications that run code from the heap. If - /// HEAP_CREATE_ENABLE_EXECUTE is not specified and an application attempts to run code from a protected page, - /// the application receives an exception with the status code STATUS_ACCESS_VIOLATION. - /// - CreateEnableExecute = 0x00040000, - } - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setevent - /// Sets the specified event object to the signaled state. - /// - /// A handle to the event object. The CreateEvent or OpenEvent function returns this handle. - /// - /// If the function succeeds, the return value is nonzero. - /// If the function fails, the return value is zero. To get extended error information, call GetLastError. - /// - [DllImport("kernel32.dll")] - public static extern bool SetEvent(IntPtr hEvent); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-freelibrary. - /// Frees the loaded dynamic-link library (DLL) module and, if necessary, decrements its reference count. When the reference - /// count reaches zero, the module is unloaded from the address space of the calling process and the handle is no longer - /// valid. - /// - /// - /// A handle to the loaded library module. The LoadLibrary, LoadLibraryEx, GetModuleHandle, or GetModuleHandleEx function - /// returns this handle. - /// - /// - /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended - /// error information, call the GetLastError function. - /// - [DllImport("kernel32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool FreeLibrary(IntPtr hModule); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew. - /// Retrieves the fully qualified path for the file that contains the specified module. The module must have been loaded - /// by the current process. To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx - /// function. - /// - /// - /// A handle to the loaded module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves - /// the path of the executable file of the current process. The GetModuleFileName function does not retrieve the path - /// for modules that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx. - /// - /// - /// A pointer to a buffer that receives the fully qualified path of the module. If the length of the path is less than - /// the size that the nSize parameter specifies, the function succeeds and the path is returned as a null-terminated - /// string. If the length of the path exceeds the size that the nSize parameter specifies, the function succeeds and - /// the string is truncated to nSize characters including the terminating null character. - /// - /// - /// The size of the lpFilename buffer, in TCHARs. - /// - /// - /// If the function succeeds, the return value is the length of the string that is copied to the buffer, in characters, - /// not including the terminating null character. If the buffer is too small to hold the module name, the string is - /// truncated to nSize characters including the terminating null character, the function returns nSize, and the function - /// sets the last error to ERROR_INSUFFICIENT_BUFFER. If nSize is zero, the return value is zero and the last error - /// code is ERROR_SUCCESS. If the function fails, the return value is 0 (zero). To get extended error information, call - /// GetLastError. - /// - [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] - [PreserveSig] - public static extern uint GetModuleFileNameW( - [In] IntPtr hModule, - [Out] StringBuilder lpFilename, - [In][MarshalAs(UnmanagedType.U4)] int nSize); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandlew. - /// Retrieves a module handle for the specified module. The module must have been loaded by the calling process. To - /// avoid the race conditions described in the Remarks section, use the GetModuleHandleEx function. - /// - /// - /// The name of the loaded module (either a .dll or .exe file). If the file name extension is omitted, the default - /// library extension .dll is appended. The file name string can include a trailing point character (.) to indicate - /// that the module name has no extension. The string does not have to specify a path. When specifying a path, be sure - /// to use backslashes (\), not forward slashes (/). The name is compared (case independently) to the names of modules - /// currently mapped into the address space of the calling process. If this parameter is NULL, GetModuleHandle returns - /// a handle to the file used to create the calling process (.exe file). The GetModuleHandle function does not retrieve - /// handles for modules that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag.For more information, see LoadLibraryEx. - /// - /// - /// If the function succeeds, the return value is a handle to the specified module. If the function fails, the return - /// value is NULL.To get extended error information, call GetLastError. - /// - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - public static extern IntPtr GetModuleHandleW(string lpModuleName); - - /// - /// Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL). - /// - /// - /// A handle to the DLL module that contains the function or variable. The LoadLibrary, LoadLibraryEx, LoadPackagedLibrary, - /// or GetModuleHandle function returns this handle. The GetProcAddress function does not retrieve addresses from modules - /// that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag.For more information, see LoadLibraryEx. - /// - /// - /// The function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be - /// in the low-order word; the high-order word must be zero. - /// - /// - /// If the function succeeds, the return value is the address of the exported function or variable. If the function - /// fails, the return value is NULL.To get extended error information, call GetLastError. - /// - [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true)] - [SuppressMessage("Globalization", "CA2101:Specify marshaling for P/Invoke string arguments", Justification = "Ansi only")] - public static extern IntPtr GetProcAddress(IntPtr hModule, string procName); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryw. - /// Loads the specified module into the address space of the calling process. The specified module may cause other modules - /// to be loaded. For additional load options, use the LoadLibraryEx function. - /// - /// - /// The name of the module. This can be either a library module (a .dll file) or an executable module (an .exe file). - /// The name specified is the file name of the module and is not related to the name stored in the library module itself, - /// as specified by the LIBRARY keyword in the module-definition (.def) file. If the string specifies a full path, the - /// function searches only that path for the module. If the string specifies a relative path or a module name without - /// a path, the function uses a standard search strategy to find the module; for more information, see the Remarks. - /// If the function cannot find the module, the function fails.When specifying a path, be sure to use backslashes (\), - /// not forward slashes(/). For more information about paths, see Naming a File or Directory. If the string specifies - /// a module name without a path and the file name extension is omitted, the function appends the default library extension - /// .dll to the module name. To prevent the function from appending .dll to the module name, include a trailing point - /// character (.) in the module name string. - /// - /// - /// If the function succeeds, the return value is a handle to the module. If the function fails, the return value is - /// NULL.To get extended error information, call GetLastError. - /// - [DllImport("kernel32", CharSet = CharSet.Unicode)] - public static extern IntPtr LoadLibraryW([MarshalAs(UnmanagedType.LPWStr)] string lpFileName); - - /// - /// ReadProcessMemory copies the data in the specified address range from the address space of the specified process - /// into the specified buffer of the current process. Any process that has a handle with PROCESS_VM_READ access can - /// call the function. The entire area to be read must be accessible, and if it is not accessible, the function fails. - /// - /// - /// A handle to the process with memory that is being read. The handle must have PROCESS_VM_READ access to the process. - /// - /// - /// A pointer to the base address in the specified process from which to read. Before any data transfer occurs, the - /// system verifies that all data in the base address and memory of the specified size is accessible for read access, - /// and if it is not accessible the function fails. - /// - /// - /// A pointer to a buffer that receives the contents from the address space of the specified process. - /// - /// - /// The number of bytes to be read from the specified process. - /// - /// - /// A pointer to a variable that receives the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead - /// is NULL, the parameter is ignored. - /// - /// - /// If the function succeeds, the return value is nonzero. If the function fails, the return value is 0 (zero). To get - /// extended error information, call GetLastError. The function fails if the requested read operation crosses into an - /// area of the process that is inaccessible. - /// - [DllImport("kernel32.dll")] - public static extern bool ReadProcessMemory( - IntPtr hProcess, - IntPtr lpBaseAddress, - IntPtr lpBuffer, - int dwSize, - out IntPtr lpNumberOfBytesRead); - - /// - /// ReadProcessMemory copies the data in the specified address range from the address space of the specified process - /// into the specified buffer of the current process. Any process that has a handle with PROCESS_VM_READ access can - /// call the function. The entire area to be read must be accessible, and if it is not accessible, the function fails. - /// - /// - /// A handle to the process with memory that is being read. The handle must have PROCESS_VM_READ access to the process. - /// - /// - /// A pointer to the base address in the specified process from which to read. Before any data transfer occurs, the - /// system verifies that all data in the base address and memory of the specified size is accessible for read access, - /// and if it is not accessible the function fails. - /// - /// - /// A pointer to a buffer that receives the contents from the address space of the specified process. - /// - /// - /// The number of bytes to be read from the specified process. - /// - /// - /// A pointer to a variable that receives the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead - /// is NULL, the parameter is ignored. - /// - /// - /// If the function succeeds, the return value is nonzero. If the function fails, the return value is 0 (zero). To get - /// extended error information, call GetLastError. The function fails if the requested read operation crosses into an - /// area of the process that is inaccessible. - /// - [DllImport("kernel32.dll")] - public static extern bool ReadProcessMemory( - IntPtr hProcess, - IntPtr lpBaseAddress, - byte[] lpBuffer, - int dwSize, - out IntPtr lpNumberOfBytesRead); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-seterrormode. - /// Controls whether the system will handle the specified types of serious errors or whether the process will handle - /// them. - /// - /// - /// The process error mode. This parameter can be one or more of the ErrorMode enum values. - /// - /// - /// The return value is the previous state of the error-mode bit flags. - /// - [DllImport("kernel32.dll")] - public static extern ErrorModes SetErrorMode(ErrorModes uMode); - - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-setunhandledexceptionfilter. - /// Enables an application to supersede the top-level exception handler of each thread of a process. After calling this - /// function, if an exception occurs in a process that is not being debugged, and the exception makes it to the unhandled - /// exception filter, that filter will call the exception filter function specified by the lpTopLevelExceptionFilter - /// parameter. - /// - /// - /// A pointer to a top-level exception filter function that will be called whenever the UnhandledExceptionFilter function - /// gets control, and the process is not being debugged. A value of NULL for this parameter specifies default handling - /// within UnhandledExceptionFilter. The filter function has syntax similar to that of UnhandledExceptionFilter: It - /// takes a single parameter of type LPEXCEPTION_POINTERS, has a WINAPI calling convention, and returns a value of type - /// LONG. The filter function should return one of the EXCEPTION_* enum values. - /// - /// - /// The SetUnhandledExceptionFilter function returns the address of the previous exception filter established with the - /// function. A NULL return value means that there is no current top-level exception handler. - /// - [DllImport("kernel32.dll")] - public static extern IntPtr SetUnhandledExceptionFilter(IntPtr lpTopLevelExceptionFilter); - - /// - /// HeapCreate - Creates a private heap object that can be used by the calling process. - /// The function reserves space in the virtual address space of the process and allocates - /// physical storage for a specified initial portion of this block. - /// Ref: https://learn.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapcreate - /// - /// - /// The heap allocation options. - /// These options affect subsequent access to the new heap through calls to the heap functions. - /// - /// - /// The initial size of the heap, in bytes. - /// - /// This value determines the initial amount of memory that is committed for the heap. - /// The value is rounded up to a multiple of the system page size. The value must be smaller than dwMaximumSize. - /// - /// If this parameter is 0, the function commits one page. To determine the size of a page on the host computer, - /// use the GetSystemInfo function. - /// - /// - /// The maximum size of the heap, in bytes. The HeapCreate function rounds dwMaximumSize up to a multiple of the - /// system page size and then reserves a block of that size in the process's virtual address space for the heap. - /// If allocation requests made by the HeapAlloc or HeapReAlloc functions exceed the size specified by - /// dwInitialSize, the system commits additional pages of memory for the heap, up to the heap's maximum size. - /// - /// If dwMaximumSize is not zero, the heap size is fixed and cannot grow beyond the maximum size. Also, the largest - /// memory block that can be allocated from the heap is slightly less than 512 KB for a 32-bit process and slightly - /// less than 1,024 KB for a 64-bit process. Requests to allocate larger blocks fail, even if the maximum size of - /// the heap is large enough to contain the block. - /// - /// If dwMaximumSize is 0, the heap can grow in size. The heap's size is limited only by the available memory. - /// Requests to allocate memory blocks larger than the limit for a fixed-size heap do not automatically fail; - /// instead, the system calls the VirtualAlloc function to obtain the memory that is needed for large blocks. - /// Applications that need to allocate large memory blocks should set dwMaximumSize to 0. - /// - /// - /// If the function succeeds, the return value is a handle to the newly created heap. - /// - /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. - /// - [DllImport("kernel32.dll")] - public static extern nint HeapCreate(HeapOptions flOptions, nuint dwInitialSize, nuint dwMaximumSize); - - /// - /// Allocates a block of memory from a heap. The allocated memory is not movable. - /// - /// - /// A handle to the heap from which the memory will be allocated. This handle is returned by the HeapCreate or - /// GetProcessHeap function. - /// - /// - /// The heap allocation options. Specifying any of these values will override the corresponding value specified when - /// the heap was created with HeapCreate. - /// - /// The number of bytes to be allocated. - /// - /// If the heap specified by the hHeap parameter is a "non-growable" heap, dwBytes must be less than 0x7FFF8. - /// You create a non-growable heap by calling the HeapCreate function with a nonzero value. - /// - /// - /// If the function succeeds, the return value is a pointer to the allocated memory block. - /// - /// If the function fails and you have not specified HEAP_GENERATE_EXCEPTIONS, the return value is NULL. - /// - /// If the function fails and you have specified HEAP_GENERATE_EXCEPTIONS, the function may generate either of the - /// exceptions listed in the following table. The particular exception depends upon the nature of the heap - /// corruption. For more information, see GetExceptionCode. - /// - [DllImport("kernel32.dll", SetLastError=false)] - public static extern nint HeapAlloc(nint hHeap, HeapOptions dwFlags, nuint dwBytes); - - /// - /// Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or - /// the operation fails. - /// - /// - /// A handle to the process memory to be modified. The handle must have PROCESS_VM_WRITE and PROCESS_VM_OPERATION access - /// to the process. - /// - /// - /// A pointer to the base address in the specified process to which data is written. Before data transfer occurs, the - /// system verifies that all data in the base address and memory of the specified size is accessible for write access, - /// and if it is not accessible, the function fails. - /// - /// - /// A pointer to the buffer that contains data to be written in the address space of the specified process. - /// - /// - /// The number of bytes to be written to the specified process. - /// - /// - /// A pointer to a variable that receives the number of bytes transferred into the specified process. This parameter - /// is optional. If lpNumberOfBytesWritten is NULL, the parameter is ignored. - /// - /// - /// If the function succeeds, the return value is nonzero. If the function fails, the return value is 0 (zero). To get - /// extended error information, call GetLastError.The function fails if the requested write operation crosses into an - /// area of the process that is inaccessible. - /// - [DllImport("kernel32.dll")] - public static extern bool WriteProcessMemory( - IntPtr hProcess, - IntPtr lpBaseAddress, - byte[] lpBuffer, - int dwSize, - out IntPtr lpNumberOfBytesWritten); - - /// - /// Get a handle to the current process. - /// - /// Handle to the process. - [DllImport("kernel32.dll")] - public static extern IntPtr GetCurrentProcess(); - - /// - /// Get the current process ID. - /// - /// The process ID. - [DllImport("kernel32.dll")] - public static extern uint GetCurrentProcessId(); - - /// - /// Get the current thread ID. - /// - /// The thread ID. - [DllImport("kernel32.dll")] - public static extern uint GetCurrentThreadId(); -} - -/// -/// Native dbghelp functions. -/// -[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1201:Elements should appear in the correct order", Justification = "Native funcs")] -internal static partial class NativeFunctions -{ - /// - /// Type of minidump to create. - /// - public enum MiniDumpType : int - { - /// - /// Normal minidump. - /// - MiniDumpNormal, - - /// - /// Minidump with data segments. - /// - MiniDumpWithDataSegs, - - /// - /// Minidump with full memory. - /// - MiniDumpWithFullMemory, - } - - /// - /// Initializes the symbol handler for a process. - /// - /// - /// A handle that identifies the caller. - /// This value should be unique and nonzero, but need not be a process handle. - /// However, if you do use a process handle, be sure to use the correct handle. - /// If the application is a debugger, use the process handle for the process being debugged. - /// Do not use the handle returned by GetCurrentProcess when debugging another process, because calling functions like SymLoadModuleEx can have unexpected results. - /// This parameter cannot be NULL. - /// - /// The path, or series of paths separated by a semicolon (;), that is used to search for symbol files. - /// If this parameter is NULL, the library attempts to form a symbol path from the following sources: - /// - The current working directory of the application - /// - The _NT_SYMBOL_PATH environment variable - /// - The _NT_ALTERNATE_SYMBOL_PATH environment variable - /// Note that the search path can also be set using the SymSetSearchPath function. - /// - /// - /// If this value is , enumerates the loaded modules for the process and effectively calls the SymLoadModule64 function for each module. - /// - /// Whether or not the function succeeded. - [DllImport("dbghelp.dll", CharSet = CharSet.Auto)] - public static extern bool SymInitialize(IntPtr hProcess, string userSearchPath, bool fInvadeProcess); - - /// - /// Deallocates all resources associated with the process handle. - /// - /// A handle to the process that was originally passed to the function. - /// Whether or not the function succeeded. - [DllImport("dbghelp.dll", CharSet = CharSet.Auto)] - public static extern bool SymCleanup(IntPtr hProcess); - - /// - /// Creates a minidump. - /// - /// Target process handle. - /// Target process ID. - /// Output file handle. - /// Type of dump to take. - /// Exception information. - /// User information. - /// Callback. - /// Whether or not the minidump succeeded. - [DllImport("dbghelp.dll")] - public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, IntPtr hFile, int dumpType, ref MinidumpExceptionInformation exceptionInfo, IntPtr userStreamParam, IntPtr callback); - - /// - /// Structure describing minidump exception information. - /// - [StructLayout(LayoutKind.Sequential, Pack = 4)] - public struct MinidumpExceptionInformation - { - /// - /// ID of the thread that caused the exception. - /// - public uint ThreadId; - - /// - /// Pointer to the exception record. - /// - public IntPtr ExceptionPointers; - - /// - /// ClientPointers field. - /// - public int ClientPointers; - } - - /// - /// Finds window according to conditions. - /// - /// Handle to parent window. - /// Window to search after. - /// Name of class. - /// Name of window. - /// Found window, or null. - [DllImport("user32.dll", CharSet = CharSet.Unicode)] - public static extern IntPtr FindWindowEx( - IntPtr parentHandle, - IntPtr childAfter, - string className, - IntPtr windowTitle); -} - -/// -/// Native ws2_32 functions. -/// -internal static partial class NativeFunctions -{ - /// - /// See https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt. - /// The setsockopt function sets a socket option. - /// - /// - /// A descriptor that identifies a socket. - /// - /// - /// The level at which the option is defined (for example, SOL_SOCKET). - /// - /// - /// The socket option for which the value is to be set (for example, SO_BROADCAST). The optname parameter must be a - /// socket option defined within the specified level, or behavior is undefined. - /// - /// - /// A pointer to the buffer in which the value for the requested option is specified. - /// - /// - /// The size, in bytes, of the buffer pointed to by the optval parameter. - /// - /// - /// If no error occurs, setsockopt returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error - /// code can be retrieved by calling WSAGetLastError. - /// - [DllImport("ws2_32.dll", CallingConvention = CallingConvention.Winapi, EntryPoint = "setsockopt")] - public static extern int SetSockOpt(IntPtr socket, SocketOptionLevel level, SocketOptionName optName, ref IntPtr optVal, int optLen); -} - -/// -/// Native dwmapi functions. -/// -internal static partial class NativeFunctions -{ - /// - /// Attributes for use with DwmSetWindowAttribute. - /// - public enum DWMWINDOWATTRIBUTE : int - { - /// - /// Allows the window frame for this window to be drawn in dark mode colors when the dark mode system setting is enabled. - /// - DWMWA_USE_IMMERSIVE_DARK_MODE = 20, - } - - /// - /// Sets the value of Desktop Window Manager (DWM) non-client rendering attributes for a window. - /// - /// The handle to the window for which the attribute value is to be set. - /// The attribute to be set. - /// The value of the attribute. - /// The size of the attribute. - /// HRESULT. - [DllImport("dwmapi.dll", PreserveSig = true)] - public static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE attr, ref int attrValue, int attrSize); -} diff --git a/Dalamud/NativeMethods.txt b/Dalamud/NativeMethods.txt index d0d8e3dc0..a36542cea 100644 --- a/Dalamud/NativeMethods.txt +++ b/Dalamud/NativeMethods.txt @@ -12,6 +12,11 @@ HWND_TOPMOST HWND_NOTOPMOST SET_WINDOW_POS_FLAGS +SetEvent + +SymInitialize +SymCleanup + OpenClipboard SetClipboardData CloseClipboard @@ -22,14 +27,34 @@ GlobalLock GlobalUnlock GLOBAL_ALLOC_FLAGS -MEM_ALLOCATION_TYPE VirtualAlloc VirtualProtect VirtualFree +VirtualQuery + +SetUnhandledExceptionFilter ReadProcessMemory WriteProcessMemory +FlashWindowEx + +GetProcAddress +GetModuleHandle +GetForegroundWindow GetCurrentProcess +GetWindowThreadProcessId MessageBoxW + +SystemParametersInfo + +SystemParametersInfo + +DwmSetWindowAttribute + +setsockopt + +RegisterDragDrop +RevokeDragDrop +DragQueryFileW diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index 6ec2fbd4e..3ef1fb2fd 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -27,9 +27,9 @@ using Windows.Win32.System.Memory; using Windows.Win32.System.Ole; using Windows.Win32.UI.WindowsAndMessaging; -using static TerraFX.Interop.Windows.Windows; - +using FLASHWINFO = Windows.Win32.UI.WindowsAndMessaging.FLASHWINFO; using HWND = Windows.Win32.Foundation.HWND; +using MEMORY_BASIC_INFORMATION = Windows.Win32.System.Memory.MEMORY_BASIC_INFORMATION; using Win32_PInvoke = Windows.Win32.PInvoke; namespace Dalamud.Utility; @@ -202,14 +202,14 @@ public static class Util } MEMORY_BASIC_INFORMATION mbi; - if (VirtualQuery((void*)p, &mbi, (nuint)sizeof(MEMORY_BASIC_INFORMATION)) == 0) + if (Win32_PInvoke.VirtualQuery((void*)p, &mbi, (nuint)sizeof(MEMORY_BASIC_INFORMATION)) == 0) return $"0x{p:X}(???)"; var sb = new StringBuilder(); sb.Append($"0x{p:X}("); for (int i = 0, c = 0; i < PageProtectionFlagNames.Length; i++) { - if ((mbi.Protect & (1 << i)) == 0) + if (((uint)mbi.Protect & (1 << i)) == 0) continue; if (c++ != 0) sb.Append(" | "); @@ -577,25 +577,44 @@ public static class Util } } + /// + /// Returns true if the current application has focus, false otherwise. + /// + /// + /// If the current application is focused. + /// + public static unsafe bool ApplicationIsActivated() + { + var activatedHandle = Win32_PInvoke.GetForegroundWindow(); + if (activatedHandle == IntPtr.Zero) + return false; // No window is currently activated + + uint pid; + _ = Win32_PInvoke.GetWindowThreadProcessId(activatedHandle, &pid); + if (Marshal.GetLastWin32Error() != 0) + return false; + + return pid == Environment.ProcessId; + } + /// /// Request that Windows flash the game window to grab the user's attention. /// /// Attempt to flash even if the game is currently focused. - public static void FlashWindow(bool flashIfOpen = false) + public static unsafe void FlashWindow(bool flashIfOpen = false) { - if (NativeFunctions.ApplicationIsActivated() && !flashIfOpen) + if (ApplicationIsActivated() && !flashIfOpen) return; - var flashInfo = new NativeFunctions.FlashWindowInfo + var flashInfo = new FLASHWINFO { - Size = (uint)Marshal.SizeOf(), - Count = uint.MaxValue, - Timeout = 0, - Flags = NativeFunctions.FlashWindow.All | NativeFunctions.FlashWindow.TimerNoFG, - Hwnd = Process.GetCurrentProcess().MainWindowHandle, + cbSize = (uint)sizeof(FLASHWINFO), + uCount = uint.MaxValue, + dwTimeout = 0, + dwFlags = FLASHWINFO_FLAGS.FLASHW_ALL | FLASHWINFO_FLAGS.FLASHW_TIMERNOFG, + hwnd = new HWND(Process.GetCurrentProcess().MainWindowHandle.ToPointer()), }; - - NativeFunctions.FlashWindowEx(ref flashInfo); + Win32_PInvoke.FlashWindowEx(flashInfo); } ///