Win32Hid: Fixing boolean bug and improving value caps union.
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="hDevice"></param>
58 /// <param name="deviceInfo"></param>
59 /// <returns></returns>
60 public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo)
63 IntPtr deviceInfoBuffer = IntPtr.Zero;
67 uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
68 deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
70 int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
73 Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
78 deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
82 Debug.WriteLine("GetRawInputData failed!");
87 //Always executes, prevents memory leak
88 Marshal.FreeHGlobal(deviceInfoBuffer);
96 /// Fetch pre-parsed data corresponding to HID descriptor for the given HID device.
98 /// <param name="device"></param>
99 /// <returns></returns>
100 public static IntPtr GetPreParsedData(IntPtr hDevice)
102 uint ppDataSize = 256;
103 int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
106 Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error());
110 IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize);
111 result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
114 Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error());
123 /// <param name="device"></param>
124 /// <returns></returns>
125 public static string GetDeviceName(IntPtr device)
127 uint deviceNameSize = 256;
128 int result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize);
134 IntPtr deviceName = Marshal.AllocHGlobal((int)deviceNameSize * 2); // size is the character count not byte count
137 result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, deviceName, ref deviceNameSize);
140 return Marshal.PtrToStringAnsi(deviceName, result - 1); // -1 for NULL termination
147 Marshal.FreeHGlobal(deviceName);