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@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@10: 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@10: } sl@10: }