Skip to content

Generate GetWindowLongPtr for 32-bit arch as well, with a thunk that chooses based on runtime architecture #343

Description

@jnm2

This is what I've always written by hand for AnyCPU builds (which is all builds, due to the viral nature of using anything other than AnyCPU being a headache):

[DllImport("user32.dll", EntryPoint="GetWindowLong", SetLastError=true)]
private static extern int GetWindowLong_x86(IntPtr hWnd, GWL nIndex);

[DllImport("user32.dll", EntryPoint="GetWindowLongPtr", SetLastError=true)]
private static extern IntPtr GetWindowLongPtrImpl_x64(IntPtr hWnd, GWL nIndex);

public static IntPtr GetWindowLongPtr(IntPtr hWnd, GWL nIndex)
{
    return IntPtr.Size == 4 ? (IntPtr)GetWindowLong_x86(hWnd, nIndex) : GetWindowLongPtrImpl_x64(hWnd, nIndex);
}

[DllImport("user32.dll", EntryPoint="SetWindowLong", SetLastError=true)]
private static extern int SetWindowLong_x86(IntPtr hWnd, GWL nIndex, int dwNewLong);

[DllImport("user32.dll", EntryPoint="SetWindowLongPtr", SetLastError=true)]
private static extern IntPtr SetWindowLongPtr_x64(IntPtr hWnd, GWL nIndex, IntPtr dwNewLong);

public static IntPtr SetWindowLongPtr(IntPtr hWnd, GWL nIndex, IntPtr dwNewLong)
{
    return IntPtr.Size == 4 ? (IntPtr)SetWindowLong_x86(hWnd, nIndex, (int)dwNewLong) : SetWindowLongPtr_x64(hWnd, nIndex, dwNewLong);
}

Could CsWin32 conceivably generate equivalent methods that dispatch at runtime based on architecture when the project is AnyCPU, a necessity for AnyCPU assemblies?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions