internal EgsDeviceHidReportsUpdateByWin32RawInput() { latestRawInputKind = NativeMethods.RawInputHeaderType.None; rawInputDataBufferSize = 0; reportAsByteArrayActualLength = 0; int bRawDataOfMouseMaxLength = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER)) + Marshal.SizeOf(typeof(NativeMethods.RAWMOUSE)); int offsetForHid = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER)) + Marshal.SizeOf(typeof(NativeMethods.RAWHID)); int bRawDataOfMaxHid64BytesLength = offsetForHid + 64; int bRawDataMaxLength = Math.Max(bRawDataOfMouseMaxLength, bRawDataOfMaxHid64BytesLength); bRawData = new byte[bRawDataMaxLength]; int touchReportMaxLength = Math.Max(Marshal.SizeOf(typeof(NativeMethods.RAWMOUSE)), 64); reportAsByteArray = new byte[64]; }
bool CopyRawInputToDataByteArray(IntPtr lParam) { int dwSize = 0; int rawInputHeaderSize = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER)); //If pData is NULL, the required size of the buffer is returned in *pcbSize. NativeMethods.GetRawInputData(lParam, (uint)NativeMethods.RID.INPUT, IntPtr.Zero, ref dwSize, rawInputHeaderSize); if (dwSize > rawInputDataBufferSize) { rawInputDataBufferSize = dwSize; ReleaseRawInputPtr(); } if (rawInputPtr == IntPtr.Zero) { // allocate unmanaged memory, must be released by Marshal.FreeHGlobal in ReleaseRawInputDataBuffer() rawInputPtr = Marshal.AllocHGlobal((int)rawInputDataBufferSize); if (rawInputPtr == IntPtr.Zero) { if (ApplicationCommonSettings.IsDebugging) { Debugger.Break(); } return(false); } } try { if (NativeMethods.GetRawInputData(lParam, (uint)NativeMethods.RID.INPUT, rawInputPtr, ref dwSize, rawInputHeaderSize) != dwSize) { if (ApplicationCommonSettings.IsDebugging) { Debugger.Break(); } return(false); } rawInput = (NativeMethods.RAWINPUT)Marshal.PtrToStructure(rawInputPtr, typeof(NativeMethods.RAWINPUT)); //Debug.WriteLine("rawInput.header.hDevice: " + rawInput.header.hDevice); latestRawInputKind = rawInput.header.dwType; int offset = 0; reportAsByteArrayActualLength = 0; switch (latestRawInputKind) { case NativeMethods.RawInputHeaderType.Hid: offset = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER)) + Marshal.SizeOf(typeof(NativeMethods.RAWHID)); reportAsByteArrayActualLength = rawInput.hid.dwSizeHid; break; case NativeMethods.RawInputHeaderType.Mouse: offset = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER)); reportAsByteArrayActualLength = Marshal.SizeOf(typeof(NativeMethods.RAWMOUSE)); break; case NativeMethods.RawInputHeaderType.Keyboard: return(false); break; default: return(false); break; } if (reportAsByteArrayActualLength > 64) { return(false); } Marshal.Copy(rawInputPtr, bRawData, 0, offset + reportAsByteArrayActualLength); Array.Copy(bRawData, offset, reportAsByteArray, 0, reportAsByteArrayActualLength); return(true); } catch (Exception ex) { if (ApplicationCommonSettings.IsDebugging) { Debugger.Break(); } Debug.WriteLine(ex.Message); return(false); } }