C# 在Windows10 UWP应用程序上使用SendInput发送滚动命令

C# 在Windows10 UWP应用程序上使用SendInput发送滚动命令,c#,winapi,uwp,windows-10,sendinput,C#,Winapi,Uwp,Windows 10,Sendinput,我的代码非常类似于在windows托盘应用程序中运行的代码,即使使用问题中的代码,我也会得到相同的行为。它在经典的windows应用程序(如Firefox、Chrome、windows Explorer等)上运行良好。但是,当鼠标焦点转到UWP应用程序(如Edge、Calendar或Mail)时,滚动会变得不稳定,在执行几十次滚动后,我的应用程序会挂起,甚至无法从任务管理器终止(权限被拒绝),这种行为很容易重复 我将把问题中的代码粘贴到这里: using System; using Syste

我的代码非常类似于在windows托盘应用程序中运行的代码,即使使用问题中的代码,我也会得到相同的行为。它在经典的windows应用程序(如Firefox、Chrome、windows Explorer等)上运行良好。但是,当鼠标焦点转到UWP应用程序(如Edge、Calendar或Mail)时,滚动会变得不稳定,在执行几十次滚动后,我的应用程序会挂起,甚至无法从任务管理器终止(权限被拒绝),这种行为很容易重复

我将把问题中的代码粘贴到这里:

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);

} }
有可能我正在处理windows中的错误吗?有什么能让我知道发生了什么事吗

更新:

<>我在C++中创建了一个测试Win32应用程序,以便更容易地重现/演示问题。问题在于send命令,它在任何经典应用程序处于焦点时执行时都可以正常工作。但是,当UWP应用程序处于焦点时执行,或者甚至windows启动/开始菜单都会导致调用应用程序(我的应用程序)挂起并被卡住,直到windows重新启动

对于这个问题,一个有效的解决方法是在处理钩子回调的线程的另一个线程上执行SendCommand调用。立即启动执行SendCommand的线程并从钩子回调返回将产生所需的行为,并且不会导致任何问题

if (hookStruct.flags != -1) //prevents recursive call to self
这很重要,是的。但该声明如何发挥作用尚不清楚。此字段的预期值为0、1或3。从不-1。您可能被机器上另一个活动的鼠标挂钩误导了

现在,WinRT应用程序是否在前台确实很重要。由于随后涉及到MessageBroker,并且字段值发生了更改,因此打开了。WinRT应用程序在以较低完整性级别运行的沙箱中运行。因此,该字段将不是-1,并且SendInput()调用将再次触发鼠标挂钩。一次又一次,当它用完堆栈时,表演就结束了

因此,第一种可能的修复方法是按预期使用字段,将语句更改为:

if ((hookStruct.flags & 1) == 0)
如果假定假定的WOKY鼠标钩子破坏了该字段,那么P>将不起作用,然后考虑在类中使用<代码>静态< /代码> BoOL字段来中断递归。在SendInput()调用之前将其设置为true,之后将其设置为false



但我想我知道你为什么要这么做,我也曾是一个轨迹板的受害者,它把它倒过来了。有一种更简单的方法,只需修改小斧头即可。

@hatchet我知道这一点。我的应用程序不是UWP,它是一个经典的桌面应用程序。这可能与SetWindowsHookEx的32/64位问题有关。您可以通过创建第二个线程并在该线程的上下文中运行SetWindowsHookEx来绕过它。请参见32/64和消息泵的讨论:C++的应用程序在哪里?谢谢你的回答。实际上,它没有进入递归。我查过了。我的代码与示例不同,其目的与完全反转卷轴不同。但这个例子导致了同样的问题。不管是我的情况还是你的情况,SendCommand调用在执行了几次之后,而不是在第一次执行时导致了问题。唉,你为什么要浪费我的空闲时间在你实际上没有使用的代码和拒绝记录的原因上呢??这不是免费的,我可以帮助别人。不,在“几次执行”之后,它不会导致问题,清理操作系统堆栈需要的不仅仅是这些。