RawInput.cs
author sl
Fri, 07 Nov 2014 22:12:22 +0100
changeset 10 17f8421146ba
child 17 8f7e35c3bfd1
permissions -rw-r--r--
More cleanup and re-org.
     1 using System;
     2 using System.Runtime.InteropServices;
     3 using System.Diagnostics;
     4 
     5 
     6 namespace Win32
     7 {
     8     static class RawInput
     9     {
    10         /// <summary>
    11         /// 
    12         /// </summary>
    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)
    18         {
    19             bool success = true;
    20             rawInputBuffer = IntPtr.Zero;
    21 
    22             try
    23             {
    24                 uint dwSize = 0;
    25                 uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
    26 
    27                 //Get the size of our raw input data.
    28                 Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader);
    29 
    30                 //Allocate a large enough buffer
    31                  rawInputBuffer = Marshal.AllocHGlobal((int)dwSize);
    32 
    33                 //Now read our RAWINPUT data
    34                 if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
    35                 {
    36                     return false;
    37                 }
    38 
    39                 //Cast our buffer
    40                 aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT));
    41             }
    42             catch
    43             {
    44                 Debug.WriteLine("GetRawInputData failed!");
    45                 success = false;
    46             }
    47 
    48             return success;
    49         }
    50 
    51         /// <summary>
    52         /// 
    53         /// </summary>
    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)
    59         {
    60             bool success = true;
    61             IntPtr deviceInfoBuffer = IntPtr.Zero;
    62             try
    63             {
    64                 //Get Device Info
    65                 uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
    66                 deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
    67 
    68                 int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
    69                 if (res <= 0)
    70                 {
    71                     Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
    72                     return false;
    73                 }
    74 
    75                 //Cast our buffer
    76                 deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
    77             }
    78             catch
    79             {
    80                 Debug.WriteLine("GetRawInputData failed!");
    81                 success = false;
    82             }
    83             finally
    84             {
    85                 //Always executes, prevents memory leak
    86                 Marshal.FreeHGlobal(deviceInfoBuffer);
    87             }
    88 
    89             
    90             return success;
    91         }
    92 
    93     }
    94 }