Skip to content

Commit ce8e330

Browse files
authored
Fix GetWindowLongPtr on x86 (#495)
1 parent a554d79 commit ce8e330

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/Windowing/Silk.NET.GLFW/GlfwNativeWindow.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.InteropServices;
@@ -13,9 +13,23 @@ public struct GlfwNativeWindow : INativeWindow
1313
[DllImport("user32", EntryPoint = "GetDC")]
1414
private static extern nint Win32GetDC(nint hwnd);
1515

16-
[DllImport("user32", EntryPoint = "GetWindowLongPtrA")]
17-
private static extern nint Win32GetWindowLongPtr(nint hwnd, int index);
18-
16+
17+
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
18+
private static extern nint GetWindowLongPtr32(nint hWnd, int nIndex);
19+
20+
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
21+
private static extern nint GetWindowLongPtr64(nint hWnd, int nIndex);
22+
23+
// This static method is required because Win32 does not support
24+
// GetWindowLongPtr directly
25+
private static unsafe nint GetWindowLongPtr(nint hWnd, int nIndex)
26+
{
27+
if (sizeof(nint) == 8)
28+
return GetWindowLongPtr64(hWnd, nIndex);
29+
else
30+
return GetWindowLongPtr32(hWnd, nIndex);
31+
}
32+
1933
public unsafe GlfwNativeWindow(Glfw api, WindowHandle* window) : this()
2034
{
2135
Kind |= NativeWindowFlags.Glfw;
@@ -24,7 +38,7 @@ public unsafe GlfwNativeWindow(Glfw api, WindowHandle* window) : this()
2438
{
2539
var hwnd = ((delegate* unmanaged[Cdecl]<WindowHandle*, nint>) getHwnd)(window);
2640
Kind |= NativeWindowFlags.Win32;
27-
Win32 = (hwnd, Win32GetDC(hwnd), Win32GetWindowLongPtr(hwnd, GwlpHInstance));
41+
Win32 = (hwnd, Win32GetDC(hwnd), GetWindowLongPtr(hwnd, GwlpHInstance));
2842
return;
2943
}
3044

0 commit comments

Comments
 (0)