-
Notifications
You must be signed in to change notification settings - Fork 337
Closed
Description
Let's consider fragment of class AvalonDock.Win32Helper:
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
public static void SetOwner(IntPtr childHandle, IntPtr ownerHandle)
{
SetWindowLong(childHandle,
-8, // GWL_HWNDPARENT
ownerHandle.ToInt32());
}
public static IntPtr GetOwner(IntPtr childHandle)
{
return new IntPtr(GetWindowLong(childHandle, -8));
}
Functions SetOwner/GetOwner may not work properly on x64 architecture, due we should use SetWindowLongPtr on x64-architecture.
It can be fixed in a such way:
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern int GetWindowLong32(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex) => IntPtr.Size == 8
? GetWindowLongPtr64(hWnd, nIndex)
: new IntPtr(GetWindowLong32(hWnd, nIndex));
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong) => IntPtr.Size == 8
? SetWindowLongPtr64(hWnd, nIndex, dwNewLong)
: new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
public static void SetOwner(IntPtr childHandle, IntPtr ownerHandle)
{
SetWindowLongPtr(
childHandle,
-8, // GWL_HWNDPARENT
ownerHandle);
}
public static IntPtr GetOwner(IntPtr childHandle) => GetWindowLongPtr(childHandle, -8);
Metadata
Metadata
Assignees
Labels
No labels