Starting to remove special case for app commands.
Adding a whole bunch of consumer controls.
2 using System.Runtime.InteropServices;
3 using System.Diagnostics;
9 /// Provide some utility functions for raw input handling.
16 /// <param name="aRawInputHandle"></param>
17 /// <param name="aRawInput"></param>
18 /// <param name="rawInputBuffer">Caller must free up memory on the pointer using Marshal.FreeHGlobal</param>
19 /// <returns></returns>
20 public static bool GetRawInputData(IntPtr aRawInputHandle, ref RAWINPUT aRawInput, ref IntPtr rawInputBuffer)
23 rawInputBuffer = IntPtr.Zero;
28 uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
30 //Get the size of our raw input data.
31 Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader);
33 //Allocate a large enough buffer
34 rawInputBuffer = Marshal.AllocHGlobal((int)dwSize);
36 //Now read our RAWINPUT data
37 if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
43 aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT));
47 Debug.WriteLine("GetRawInputData failed!");
57 /// <param name="aRawInputHandle"></param>
58 /// <param name="aUsagePage"></param>
59 /// <param name="aUsage"></param>
60 /// <returns></returns>
61 public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo)
64 IntPtr deviceInfoBuffer = IntPtr.Zero;
68 uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
69 deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
71 int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
74 Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
79 deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
83 Debug.WriteLine("GetRawInputData failed!");
88 //Always executes, prevents memory leak
89 Marshal.FreeHGlobal(deviceInfoBuffer);
97 /// Fetch pre-parsed data corresponding to HID descriptor for the given HID device.
99 /// <param name="device"></param>
100 /// <returns></returns>
101 public static IntPtr GetPreParsedData(IntPtr hDevice)
103 uint ppDataSize = 256;
104 int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
107 Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error());
111 IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize);
112 result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
115 Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error());