Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cuellius opened this issue Feb 22, 2022 · 2 comments

Comments

@cuellius
Copy link
Contributor

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);
@Dirkster99
Copy link
Owner

Thank you for these insights - I was not aware of this issue and am no expert for this Windows specific area of programming :-) Could you please prepare a pull request ? I would be more than happy to add the fix :-)

@cuellius
Copy link
Contributor Author

Ok, I have done it.

Dirkster99 added a commit that referenced this issue Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants