Improved P/Invoke bindings for GetWindowLongPtr() and SetWindowLongPtr() functions.

This commit is contained in:
LoRd_MuldeR 2022-11-10 00:09:23 +01:00
parent 7fee4f9bf3
commit 5759a0cd83
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 52 additions and 18 deletions

View File

@ -325,9 +325,9 @@ namespace com.muldersoft.slunkcrypt.gui.utils.cpu
private static class Internal private static class Internal
{ {
const string DLL_NAME_X86 = "cpu-capabilities-x86.dll"; private const string DLL_NAME_X86 = "cpu-capabilities-x86.dll";
const string DLL_NAME_X64 = "cpu-capabilities-x64.dll"; private const string DLL_NAME_X64 = "cpu-capabilities-x64.dll";
const string DLL_KERNEL32 = "kernel32.dll"; private const string DLL_KERNEL32 = "kernel32.dll";
/* GetCPUArchitecture() */ /* GetCPUArchitecture() */
[DllImport(DLL_NAME_X64, CallingConvention = CallingConvention.Cdecl)] [DllImport(DLL_NAME_X64, CallingConvention = CallingConvention.Cdecl)]
@ -372,9 +372,9 @@ namespace com.muldersoft.slunkcrypt.gui.utils.cpu
public static extern uint GetCPULibraryVersionX86(); public static extern uint GetCPULibraryVersionX86();
/* IsWow64Process2() */ /* IsWow64Process2() */
[DllImport(DLL_KERNEL32, SetLastError = true)] [DllImport(DLL_KERNEL32, EntryPoint = "IsWow64Process2", ExactSpelling = true)]
public static extern bool IsWow64Process2(IntPtr process, out ushort processMachine, out ushort nativeMachine); public static extern bool IsWow64Process2(IntPtr process, out ushort processMachine, out ushort nativeMachine);
[DllImport(DLL_KERNEL32)] [DllImport(DLL_KERNEL32, EntryPoint = "GetCurrentProcess", ExactSpelling = true)]
public static extern IntPtr GetCurrentProcess(); public static extern IntPtr GetCurrentProcess();
} }

View File

@ -160,6 +160,8 @@ namespace com.muldersoft.slunkcrypt.gui.utils
private class Native private class Native
{ {
private const string DLL_USER32 = "user32.dll";
[Flags] [Flags]
public enum MenuFlags : uint public enum MenuFlags : uint
{ {
@ -171,13 +173,13 @@ namespace com.muldersoft.slunkcrypt.gui.utils
MF_SEPARATOR = 0x0800 MF_SEPARATOR = 0x0800
} }
[DllImport("user32.dll")] [DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "GetSystemMenu", ExactSpelling = true)]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll", CharSet = CharSet.Auto)] [DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "AppendMenuW", ExactSpelling = true)]
public static extern bool AppendMenu(IntPtr hMenu, MenuFlags uFlags, UIntPtr uIDNewItem, string lpNewItem); public static extern bool AppendMenu(IntPtr hMenu, MenuFlags uFlags, UIntPtr uIDNewItem, string lpNewItem);
[DllImport("user32.dll")] [DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "ModifyMenuW", ExactSpelling = true)]
public static extern bool ModifyMenu(IntPtr hMenu, uint uPosition, MenuFlags uFlags, UIntPtr uIDNewItem, string lpNewItem); public static extern bool ModifyMenu(IntPtr hMenu, uint uPosition, MenuFlags uFlags, UIntPtr uIDNewItem, string lpNewItem);
} }
} }

View File

@ -35,14 +35,14 @@ namespace com.muldersoft.slunkcrypt.gui.utils
if (!ReferenceEquals(window, null)) if (!ReferenceEquals(window, null))
{ {
const int GWL_STYLE = -16; const int GWL_STYLE = -16;
const uint WS_MAXIMIZEBOX = 0x10000; const ulong WS_MAXIMIZEBOX = 0x10000;
try try
{ {
WindowInteropHelper interopHelper = new WindowInteropHelper(window); WindowInteropHelper interopHelper = new WindowInteropHelper(window);
uint value = NativeMethods.GetWindowLong(interopHelper.Handle, GWL_STYLE); ulong value = NativeMethods.GetWindowLongPtr(interopHelper.Handle, GWL_STYLE);
if ((value & WS_MAXIMIZEBOX) != 0U) if (value != 0UL)
{ {
NativeMethods.SetWindowLong(interopHelper.Handle, GWL_STYLE, value & (~WS_MAXIMIZEBOX)); NativeMethods.SetWindowLongPtr(interopHelper.Handle, GWL_STYLE, value & (~WS_MAXIMIZEBOX));
} }
} }
catch { } catch { }
@ -51,17 +51,49 @@ namespace com.muldersoft.slunkcrypt.gui.utils
private static class NativeMethods private static class NativeMethods
{ {
[DllImport("user32.dll", SetLastError = true)] private const string DLL_USER32 = "user32.dll";
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "BringWindowToTop", ExactSpelling = true)]
public static extern bool BringWindowToTop(IntPtr hWnd); public static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)] [DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "SetForegroundWindow", ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hWnd); public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)] [DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "GetWindowLongW", ExactSpelling = true)]
public static extern uint GetWindowLong(IntPtr hWnd, int nIndex); private static extern UIntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)] [DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "GetWindowLongPtrW", ExactSpelling = true)]
public static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); private static extern UIntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "SetWindowLongW", ExactSpelling = true)]
private static extern UIntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, UIntPtr dwNewLong);
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)]
private static extern UIntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, UIntPtr dwNewLong);
public static ulong GetWindowLongPtr(IntPtr hWnd, int nIndex)
{
if (IntPtr.Size >= 8)
{
return GetWindowLongPtr64(hWnd, nIndex).ToUInt64();
}
else
{
return GetWindowLongPtr32(hWnd, nIndex).ToUInt64();
}
}
public static void SetWindowLongPtr(IntPtr hWnd, int nIndex, ulong dwNewLong)
{
if (IntPtr.Size >= 8)
{
SetWindowLongPtr64(hWnd, nIndex, new UIntPtr(dwNewLong));
}
else
{
SetWindowLongPtr32(hWnd, nIndex, new UIntPtr(dwNewLong));
}
}
} }
} }
} }