IMessageFilter无法在管理权限下工作

时间:2018-05-02 11:09:10

标签: c# .net raw-input imessagefilter

我使用IMessageFilter通过鼠标和键盘跟踪用户输入。

static void Main()
{
    try
    {
         AddKeyboardMouseDevice(this.Handle);
         RegisterDevices();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Unhandled exception occurred tray application." + ex.Message);
    }
}

public void AddKeyboardMouseDevice(IntPtr windowHandle)
{
    devices.Add(new RAWINPUTDEVICE(1, 6, 0x100, windowHandle));
    devices.Add(new RAWINPUTDEVICE(1, 2, 0x100, windowHandle));
}

public bool RegisterDevices()
{
    if (devices.Count == 0) return false;
    log.DebugFormat("Registering for keyboard mouse hook");
    RAWINPUTDEVICE[] d = devices.ToArray();
    bool result = RegisterRawInputDevices(d, devices.Count, Marshal.SizeOf(typeof(RAWINPUTDEVICE)));
    return result;
}

public bool PreFilterMessage(ref Message m)
{
    if (m.Msg == WM_INPUT)
    {
        int pcbSize = Marshal.SizeOf(typeof(RawInput));

        RawInput pData = new RawInput();
        int result = GetRawInputData(m.LParam,
            RawInputCommand.Input, out pData,
            ref pcbSize, Marshal.SizeOf(typeof(RAWINPUTHEADER)));
        if (result != -1)
        {
            ProcessRawInput(pData, m.LParam);
        }
    }
    return false; 
}

但我的问题是,当进程在管理权限下运行时,我不会收到任何消息。如何为在管理权限下运行的进程获取这些消息?

0 个答案:

没有答案