Skip to content

Commit

Permalink
Fix x84 support
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLua committed Jan 9, 2025
1 parent e74ddf2 commit f71db44
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 18 additions & 2 deletions FluentAutoClicker/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ private LRESULT HotKeyProc(HWND hWnd, uint Msg, WPARAM wParam, LPARAM lParam)
return PInvoke.CallWindowProc(origHotKeyProc, hWnd, Msg, wParam, lParam);
}

[LibraryImport("user32.dll", EntryPoint = "SetWindowLongW", SetLastError = true)]
private static partial int SetWindowLong_x86(nint hWnd, int nIndex, int dwNewLong);

[LibraryImport("user32.dll", EntryPoint = "SetWindowLongPtrW", SetLastError = true)]
private static partial nint SetWindowLongPtr_x64(nint hWnd, int nIndex, nint dwNewLong);

// CsWin32 doesn't allow specifying the calling convention between x86 and x64
private static nint SetWindowLongPtr(HWND hWnd, int nIndex, nint dwNewLong)
{
nint hWndInt = hWnd.Value;
return IntPtr.Size == 4
? SetWindowLong_x86(hWndInt, nIndex, (int)dwNewLong)
: SetWindowLongPtr_x64(hWndInt, nIndex, dwNewLong);
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// Get window handle
Expand All @@ -68,9 +83,10 @@ private void MainPage_Loaded(object sender, RoutedEventArgs e)
_ = PInvoke.RegisterHotKey(hWnd, id, HOT_KEY_MODIFIERS.MOD_NOREPEAT, 0x75); // F6

// Add hotkey function pointer to window procedure
int GWL_WNDPROC = -4;
hotKeyProcD = HotKeyProc;
nint hotKeyProcPtr = Marshal.GetFunctionPointerForDelegate(hotKeyProcD);
nint wndPtr = PInvoke.SetWindowLongPtr(hWnd, WINDOW_LONG_PTR_INDEX.GWL_WNDPROC, hotKeyProcPtr);
IntPtr hotKeyProcPtr = Marshal.GetFunctionPointerForDelegate(hotKeyProcD);
IntPtr wndPtr = SetWindowLongPtr(hWnd, GWL_WNDPROC, hotKeyProcPtr);
origHotKeyProc = Marshal.GetDelegateForFunctionPointer<WNDPROC>(wndPtr);
}

Expand Down
1 change: 0 additions & 1 deletion FluentAutoClicker/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
RegisterHotKey
UnregisterHotKey
SetWindowLongPtr
CallWindowProc
SendInput

0 comments on commit f71db44

Please sign in to comment.