Skip to content

Commit 3078b19

Browse files
authored
Code Quality: Defined manual SetWindowLongPtr (#16781)
1 parent a2b9726 commit 3078b19

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ FindWindow
9898
SendMessage
9999
IsWindowVisible
100100
COPYDATASTRUCT
101-
SetWindowLongPtr
101+
WINDOW_LONG_PTR_INDEX
102102
GetDpiForWindow
103103
CallWindowProc
104104
MINMAXINFO

src/Files.App.CsWin32/Windows.Win32.Extras.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Runtime.InteropServices;
55
using Windows.Win32.Foundation;
6+
using Windows.Win32.UI.WindowsAndMessaging;
67

78
namespace Windows.Win32
89
{
@@ -17,4 +18,23 @@ namespace UI.WindowsAndMessaging
1718
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
1819
public delegate LRESULT WNDPROC(HWND hWnd, uint msg, WPARAM wParam, LPARAM lParam);
1920
}
21+
22+
public static partial class PInvoke
23+
{
24+
[DllImport("User32", EntryPoint = "SetWindowLongW", ExactSpelling = true)]
25+
static extern int _SetWindowLong(HWND hWnd, int nIndex, int dwNewLong);
26+
27+
[DllImport("User32", EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)]
28+
static extern nint _SetWindowLongPtr(HWND hWnd, int nIndex, nint dwNewLong);
29+
30+
// NOTE:
31+
// CsWin32 doesn't generate SetWindowLong on other than x86 and vice versa.
32+
// For more info, visit https://github.com/microsoft/CsWin32/issues/882
33+
public static unsafe nint SetWindowLongPtr(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, nint dwNewLong)
34+
{
35+
return sizeof(nint) is 4
36+
? (nint)_SetWindowLong(hWnd, (int)nIndex, (int)dwNewLong)
37+
: _SetWindowLongPtr(hWnd, (int)nIndex, dwNewLong);
38+
}
39+
}
2040
}

0 commit comments

Comments
 (0)