这是indexloc提供的服务,不要输入任何密码
Skip to content

x64-issue: x86-specific functions are used when project is compiled for x64-architecture #333

@cuellius

Description

@cuellius

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions