From bc8decc74847b05fd5e44e59a3fb7fc4b44f6613 Mon Sep 17 00:00:00 2001 From: Christoph Wille Date: Sat, 1 Jun 2024 15:42:56 +0200 Subject: [PATCH] Switch from DllImport to LibraryImport source generator --- ILSpy/NativeMethods.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ILSpy/NativeMethods.cs b/ILSpy/NativeMethods.cs index 8ba9b02fa..ba3e03b67 100644 --- a/ILSpy/NativeMethods.cs +++ b/ILSpy/NativeMethods.cs @@ -21,16 +21,19 @@ using System.Runtime.InteropServices; namespace ICSharpCode.ILSpy { - static class NativeMethods + // Uses https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation + internal static partial class NativeMethods { - [DllImport("dwmapi.dll", PreserveSig = true)] - public static extern int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute attr, ref int attrValue, int attrSize); + const int S_OK = 0; + + [LibraryImport("dwmapi.dll", EntryPoint = "DwmSetWindowAttribute")] + internal static partial int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute attr, ref int attrValue, int attrSize); public static bool UseImmersiveDarkMode(IntPtr hWnd, bool enable) { int darkMode = enable ? 1 : 0; - int hr = DwmSetWindowAttribute(hWnd, DwmWindowAttribute.UseImmersiveDarkMode, ref darkMode, sizeof(int)); - return hr >= 0; + int hResult = DwmSetWindowAttribute(hWnd, DwmWindowAttribute.UseImmersiveDarkMode, ref darkMode, sizeof(int)); + return hResult > S_OK; } }