I have a strange problem in a raw proc input window that I cannot understand for life.
Each time I press the Esc key, my code gets an exception 0x80004005 (invalid handle) the first time GetRawInputData() called. This seems to only happen with the Esc key :
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); }
Structure Definition:
[StructLayout(LayoutKind.Sequential)] public struct RawInputHeader { public uint dwType; public uint dwSize; public IntPtr hDevice; public IntPtr wParam; }
Is there something wrong with the proc window or some kind of weirdness around Esc and GetRawInputData() that I don't know about?
source share