HidDevice.cs
author StephaneLenclud
Sun, 15 Feb 2015 19:53:27 +0100
changeset 61 60bfe5083721
parent 60 687cace560d2
child 63 4b8b058de215
permissions -rw-r--r--
Much improved raw input device list.
     1 using System;
     2 using System.Windows.Forms;
     3 using System.Runtime.InteropServices;
     4 using System.Diagnostics;
     5 using System.Text;
     6 using Microsoft.Win32.SafeHandles;
     7 using Win32;
     8 
     9 namespace Hid
    10 {
    11     /// <summary>
    12     /// Represent a HID device.
    13     /// Rename to RawInputDevice?
    14     /// </summary>
    15     public class HidDevice: IDisposable
    16     {
    17         public string Name { get; private set; }
    18         public string Manufacturer { get; private set; }
    19         public string Product { get; private set; }
    20         public ushort VendorId { get; private set; }
    21         public ushort ProductId { get; private set; }
    22         public ushort Version { get; private set; }
    23         //Pre-parsed HID descriptor
    24         public IntPtr PreParsedData {get; private set;}
    25         //Info
    26         public RID_DEVICE_INFO Info { get {return iInfo;} }
    27         private RID_DEVICE_INFO iInfo;
    28         //Capabilities
    29         public HIDP_CAPS Capabilities { get { return iCapabilities; } }
    30         private HIDP_CAPS iCapabilities;
    31         //Input Button Capabilities
    32         public HIDP_BUTTON_CAPS[] InputButtonCapabilities { get { return iInputButtonCapabilities; } }
    33         private HIDP_BUTTON_CAPS[] iInputButtonCapabilities;
    34         //Input Value Capabilities
    35         public HIDP_VALUE_CAPS[] InputValueCapabilities { get { return iInputValueCapabilities; } }
    36         private HIDP_VALUE_CAPS[] iInputValueCapabilities;
    37 
    38         
    39         
    40 
    41         /// <summary>
    42         /// Class constructor will fetch this object properties from HID sub system.
    43         /// </summary>
    44         /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
    45         public HidDevice(IntPtr hRawInputDevice)
    46         {
    47             //Try construct and rollback if needed
    48             try
    49             {
    50                 Construct(hRawInputDevice);
    51             }
    52             catch (System.Exception ex)
    53             {
    54                 //Just rollback and propagate
    55                 Dispose();
    56                 throw ex;
    57             }
    58         }
    59 
    60 
    61         /// <summary>
    62         /// Make sure dispose is called even if the user forgot about it.
    63         /// </summary>
    64         ~HidDevice()
    65         {
    66             Dispose();
    67         }
    68 
    69         /// <summary>
    70         /// Private constructor.
    71         /// </summary>
    72         /// <param name="hRawInputDevice"></param>
    73         private void Construct(IntPtr hRawInputDevice)
    74         {
    75             PreParsedData = IntPtr.Zero;
    76             iInputButtonCapabilities = null;
    77             iInputValueCapabilities = null;
    78 
    79             //Fetch various information defining the given HID device
    80             Name = Win32.RawInput.GetDeviceName(hRawInputDevice);
    81 
    82             //Fetch device info
    83             iInfo = new RID_DEVICE_INFO();
    84             if (!Win32.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
    85             {
    86                 throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
    87             }
    88 
    89             //Open our device from the device name/path
    90             SafeFileHandle handle = Win32.Function.CreateFile(Name,
    91                 Win32.FileAccess.NONE,
    92                 Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
    93                 IntPtr.Zero,
    94                 Win32.CreationDisposition.OPEN_EXISTING,
    95                 Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
    96                 IntPtr.Zero
    97                 );
    98 
    99             //Check if CreateFile worked
   100             if (handle.IsInvalid)
   101             {
   102                 throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
   103             }
   104 
   105             //Get manufacturer string
   106             StringBuilder manufacturerString = new StringBuilder(256);
   107             if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
   108             {
   109                 Manufacturer = manufacturerString.ToString();
   110             }
   111 
   112             //Get product string
   113             StringBuilder productString = new StringBuilder(256);
   114             if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
   115             {
   116                 Product = productString.ToString();
   117             }
   118 
   119             //Get attributes
   120             Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
   121             if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
   122             {
   123                 VendorId = attributes.VendorID;
   124                 ProductId = attributes.ProductID;
   125                 Version = attributes.VersionNumber;
   126             }
   127 
   128             handle.Close();
   129 
   130             //Get our HID descriptor pre-parsed data
   131             PreParsedData = Win32.RawInput.GetPreParsedData(hRawInputDevice);
   132 
   133             if (PreParsedData == IntPtr.Zero)
   134             {
   135                 //We are done then.
   136                 //Some devices don't have pre-parsed data.
   137                 return;
   138             }
   139 
   140             //Get capabilities
   141             HidStatus status = Win32.Function.HidP_GetCaps(PreParsedData, ref iCapabilities);
   142             if (status != HidStatus.HIDP_STATUS_SUCCESS)
   143             {
   144                 throw new Exception("HidDevice: HidP_GetCaps failed: " + status.ToString());
   145             }
   146 
   147             //Get input button caps if needed
   148             if (Capabilities.NumberInputButtonCaps > 0)
   149             {
   150                 iInputButtonCapabilities = new HIDP_BUTTON_CAPS[Capabilities.NumberInputButtonCaps];
   151                 ushort buttonCapabilitiesLength = Capabilities.NumberInputButtonCaps;
   152                 status = Win32.Function.HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, iInputButtonCapabilities, ref buttonCapabilitiesLength, PreParsedData);
   153                 if (status != HidStatus.HIDP_STATUS_SUCCESS || buttonCapabilitiesLength != Capabilities.NumberInputButtonCaps)
   154                 {
   155                     throw new Exception("HidDevice: HidP_GetButtonCaps failed: " + status.ToString());
   156                 }
   157             }
   158 
   159             //Get input value caps if needed
   160             if (Capabilities.NumberInputValueCaps > 0)
   161             {
   162                 iInputValueCapabilities = new HIDP_VALUE_CAPS[Capabilities.NumberInputValueCaps];
   163                 ushort valueCapabilitiesLength = Capabilities.NumberInputValueCaps;
   164                 status = Win32.Function.HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, iInputValueCapabilities, ref valueCapabilitiesLength, PreParsedData);
   165                 if (status != HidStatus.HIDP_STATUS_SUCCESS || valueCapabilitiesLength != Capabilities.NumberInputValueCaps)
   166                 {
   167                     throw new Exception("HidDevice: HidP_GetValueCaps failed: " + status.ToString());
   168                 }
   169             }
   170 
   171 
   172         }
   173 
   174         /// <summary>
   175         /// Dispose is just for unmanaged clean-up.
   176         /// Make sure calling disposed multiple times does not crash.
   177         /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
   178         /// </summary>
   179         public void Dispose()
   180         {
   181             Marshal.FreeHGlobal(PreParsedData);
   182             PreParsedData = IntPtr.Zero;
   183         }
   184 
   185         /// <summary>
   186         /// Print information about this device to our debug output.
   187         /// </summary>
   188         public void DebugWrite()
   189         {
   190             Debug.WriteLine("================ HID =========================================================================================");
   191             Debug.WriteLine("==== Name: " + Name);
   192             Debug.WriteLine("==== Manufacturer: " + Manufacturer);
   193             Debug.WriteLine("==== Product: " + Product);
   194             Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
   195             Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
   196             Debug.WriteLine("==== Version: " + Version.ToString());
   197             Debug.WriteLine("==============================================================================================================");
   198         }
   199 
   200     }
   201 
   202 }