Sending scroll commands using SendInput in UWP apps for Windows 10

I have code very similar to this question running in a Windows-tray application, even with this exact code from the question I get the same behavior. All of this works well in classic Windows applications like Firefox, Chrome, Windows Explorer, etc. However, when the mouse focus falls on a UWP application such as Edge or Calendar or Mail, the scroll becomes unstable and after several tens of scrolls my application runs, it hangs and cannot even be terminated from the task manager (permission denied), this behavior is very reproducible.

Paste the code from the question here:

using System; using System.Diagnostics; using System.Windows.Forms; using System.Runtime.InteropServices; namespace EnableMacScrolling { class InterceptMouse { const int INPUT_MOUSE = 0; const int MOUSEEVENTF_WHEEL = 0x0800; const int WH_MOUSE_LL = 14; private static LowLevelMouseProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; public static void Main() { _hookID = SetHook(_proc); if (_hookID == null) { MessageBox.Show("SetWindowsHookEx Failed"); return; } Application.Run(); UnhookWindowsHookEx(_hookID); } private static IntPtr SetHook(LowLevelMouseProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam) { MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); Console.WriteLine(hookStruct.mouseData); if (hookStruct.flags != -1) //prevents recursive call to self { INPUT input; input = new INPUT(); input.type = INPUT_MOUSE; input.mi.dx = 0; input.mi.dy = 0; input.mi.dwFlags = MOUSEEVENTF_WHEEL; input.mi.time = 0; input.mi.dwExtraInfo = 0; input.mi.mouseData = -(hookStruct.mouseData >> 16); try { SendInput(1, ref input, Marshal.SizeOf(input)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } return (IntPtr)1; } } return CallNextHookEx(_hookID, nCode, wParam, lParam); } private enum MouseMessages { WM_MOUSEWHEEL = 0x020A } [StructLayout(LayoutKind.Sequential)] private struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] private struct MSLLHOOKSTRUCT { public POINT pt; public int mouseData; public int flags; public int time; public IntPtr dwExtraInfo; } public struct INPUT { public int type; public MOUSEINPUT mi; } [StructLayout(LayoutKind.Sequential)] public struct MOUSEINPUT { public int dx; public int dy; public int mouseData; public uint dwFlags; public int time; public int dwExtraInfo; } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("User32.dll", SetLastError = true)] public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize); } } 

Is it possible that I am dealing with a window error here? Any guidance on how I can find out what is going on?

Update:

I created a Win32 test application in C ++ to more easily reproduce / demonstrate the problem. The problem is SendCommand, which, when executed, when some classic application is in focus, works fine. However, when executed while the UWP application is in focus, or even the Windows start / start menu causes the calling application (my application) to freeze and get stuck until windows restart.

An effective workaround / solution to this problem is to make a SendCommand call to another thread from the thread that processes the hook callback. Immediately starting the thread that SendCommand executes and returning from the hook callback causes the desired behavior and does not cause any problems.

+3
source share
1 answer
 if (hookStruct.flags != -1) //prevents recursive call to self 

This is pretty important, yes. But how expression can do its job is very unclear. The expected value of this field is 0, 1, or 3. Never -1. You may have made a mistake with another mouse hook that is active on your computer.

Now it matters whether the WinRT application is in the foreground. Since then a message broker is involved and the field value changes, the LLMHF_LOWER_IL_INJECTED bit is turned on . WinRT applications run in a sandbox that runs at a lower integrity level. This way the field will not be -1, and your call to SendInput () will again call the mouse hook. Turning on and off, the show is completed when the stack ends.

So, the first possible fix is ​​to use the field, as expected, to change the statement to:

 if ((hookStruct.flags & 1) == 0) 

It will not work if this alleged mouse capture will distort the field, and then consider using the static bool field in your class to break the recursion. Set to true before calling SendInput (), after which it will be false.


But I think I know why you are doing this, I was also a victim of the trackpad that got it back. There is a much simpler way to do this, just change the FlipFlopWheel parameter.

+4
source

Source: https://habr.com/ru/post/1368867/


All Articles