使用GetRawInputData的句柄无效,但仅适用于Esc键按下

时间:2016-07-27 23:11:13

标签: c# winapi

我在原始输入窗口过程中有一个奇怪的问题,我无法弄清楚我的生活。

每按一次 Esc 键,我的代码就会在第一次const xAxis = d3.svg.axis().scale(x).orient('bottom').tickValues(input.map((d) => { if (d.chr === 'MT') { // returning a string here shows NaN return 23; } if (d.chr === 'X') { return 24; } return d.chr; })); 调用时遇到0x80004005(无效句柄)异常。它似乎只发生在 Esc 键:

GetRawInputData()

结构定义:

protected override void WndProc(ref Message message)
{
    switch ((WM)message.Msg)
    {
        case WM.INPUT:
        {
            if (message.LParam == IntPtr.Zero)
            {
                break;
            }
            InputData rawBuffer;
            var dwSize = 0;
            //Invalid handle on this call, only with escape.
            var res = User32.GetRawInputData(message.LParam, DataCommand.RID_INPUT, IntPtr.Zero, ref dwSize, Marshal.SizeOf(typeof(RawInputHeader)));
            if (res != 0)
            {
                var ex = new Win32Exception(Marshal.GetLastWin32Error());
                Logger.Error(ex, "Error sizing the rawinput buffer: {0}", ex.Message);
                break;                           
            }

            res = User32.GetRawInputData(message.LParam, DataCommand.RID_INPUT, out rawBuffer, ref dwSize, Marshal.SizeOf(typeof(RawInputHeader)));
            if (res == -1)
            {
                var ex = new Win32Exception(Marshal.GetLastWin32Error());
                Logger.Error(ex, "Error getting the rawinput buffer: {0}", ex.Message);
                break;
            }
            if (res == dwSize)
            {
                foreach (var device in _devices)
                {
                    device.ProcessRawInput(rawBuffer);
                }
            }
            else
            {
                //Something is seriously f'd up with Windows - the number of bytes copied does not match the reported buffer size.
            }
        }
        break;
    }
    base.WndProc(ref message);
}

窗口过程是否有问题,或者我不知道的 Esc [StructLayout(LayoutKind.Sequential)] public struct RawInputHeader { public uint dwType; public uint dwSize; public IntPtr hDevice; public IntPtr wParam; } 周围有些奇怪?

0 个答案:

没有答案