sl@10: using System; sl@10: using System.Runtime.InteropServices; sl@10: using System.Diagnostics; sl@10: sl@10: sl@10: namespace Win32 sl@10: { sl@17: /// sl@17: /// Provide some utility functions for raw input handling. sl@17: /// sl@10: static class RawInput sl@10: { sl@10: /// sl@10: /// sl@10: /// sl@10: /// sl@10: /// sl@10: /// Caller must free up memory on the pointer using Marshal.FreeHGlobal sl@10: /// sl@10: public static bool GetRawInputData(IntPtr aRawInputHandle, ref RAWINPUT aRawInput, ref IntPtr rawInputBuffer) sl@10: { sl@10: bool success = true; sl@10: rawInputBuffer = IntPtr.Zero; sl@10: sl@10: try sl@10: { sl@10: uint dwSize = 0; sl@10: uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)); sl@10: sl@10: //Get the size of our raw input data. sl@10: Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader); sl@10: sl@10: //Allocate a large enough buffer sl@10: rawInputBuffer = Marshal.AllocHGlobal((int)dwSize); sl@10: sl@10: //Now read our RAWINPUT data sl@10: if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize) sl@10: { sl@10: return false; sl@10: } sl@10: sl@10: //Cast our buffer sl@10: aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT)); sl@10: } sl@10: catch sl@10: { sl@10: Debug.WriteLine("GetRawInputData failed!"); sl@10: success = false; sl@10: } sl@10: sl@10: return success; sl@10: } sl@10: sl@10: /// sl@10: /// sl@10: /// sl@10: /// sl@10: /// sl@10: /// sl@10: /// sl@10: public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo) sl@10: { sl@10: bool success = true; sl@10: IntPtr deviceInfoBuffer = IntPtr.Zero; sl@10: try sl@10: { sl@10: //Get Device Info sl@10: uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO)); sl@10: deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize); sl@10: sl@23: int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize); sl@10: if (res <= 0) sl@10: { sl@10: Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString()); sl@10: return false; sl@10: } sl@10: sl@10: //Cast our buffer sl@10: deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO)); sl@10: } sl@10: catch sl@10: { sl@10: Debug.WriteLine("GetRawInputData failed!"); sl@10: success = false; sl@10: } sl@10: finally sl@10: { sl@10: //Always executes, prevents memory leak sl@10: Marshal.FreeHGlobal(deviceInfoBuffer); sl@10: } sl@10: sl@10: sl@10: return success; sl@10: } sl@10: sl@17: /// sl@19: /// Fetch pre-parsed data corresponding to HID descriptor for the given HID device. sl@17: /// sl@17: /// sl@17: /// sl@17: public static IntPtr GetPreParsedData(IntPtr hDevice) sl@17: { sl@17: uint ppDataSize = 256; sl@23: int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize); sl@17: if (result != 0) sl@17: { sl@17: Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error()); sl@17: return IntPtr.Zero; sl@17: } sl@17: sl@17: IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize); sl@23: result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize); sl@17: if (result <= 0) sl@17: { sl@17: Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error()); sl@17: return IntPtr.Zero; sl@17: } sl@17: return ppData; sl@17: } sl@17: sl@22: /// sl@22: /// sl@22: /// sl@22: /// sl@22: /// sl@22: public static string GetDeviceName(IntPtr device) sl@22: { sl@22: uint deviceNameSize = 256; sl@23: int result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize); sl@22: if (result != 0) sl@22: { sl@22: return string.Empty; sl@22: } sl@22: sl@22: IntPtr deviceName = Marshal.AllocHGlobal((int)deviceNameSize * 2); // size is the character count not byte count sl@22: try sl@22: { sl@23: result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, deviceName, ref deviceNameSize); sl@22: if (result > 0) sl@22: { sl@23: return Marshal.PtrToStringAnsi(deviceName, result - 1); // -1 for NULL termination sl@22: } sl@22: sl@22: return string.Empty; sl@22: } sl@22: finally sl@22: { sl@22: Marshal.FreeHGlobal(deviceName); sl@22: } sl@22: } sl@22: sl@17: sl@10: } sl@10: }