More cleanup and re-org.
2 using System.Runtime.InteropServices;
3 using System.Diagnostics;
13 /// <param name="aRawInputHandle"></param>
14 /// <param name="aRawInput"></param>
15 /// <param name="rawInputBuffer">Caller must free up memory on the pointer using Marshal.FreeHGlobal</param>
16 /// <returns></returns>
17 public static bool GetRawInputData(IntPtr aRawInputHandle, ref RAWINPUT aRawInput, ref IntPtr rawInputBuffer)
20 rawInputBuffer = IntPtr.Zero;
25 uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
27 //Get the size of our raw input data.
28 Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader);
30 //Allocate a large enough buffer
31 rawInputBuffer = Marshal.AllocHGlobal((int)dwSize);
33 //Now read our RAWINPUT data
34 if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
40 aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT));
44 Debug.WriteLine("GetRawInputData failed!");
54 /// <param name="aRawInputHandle"></param>
55 /// <param name="aUsagePage"></param>
56 /// <param name="aUsage"></param>
57 /// <returns></returns>
58 public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo)
61 IntPtr deviceInfoBuffer = IntPtr.Zero;
65 uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
66 deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
68 int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
71 Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
76 deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
80 Debug.WriteLine("GetRawInputData failed!");
85 //Always executes, prevents memory leak
86 Marshal.FreeHGlobal(deviceInfoBuffer);