HidDevice.cs
author StephaneLenclud
Sun, 15 Feb 2015 10:50:09 +0100
changeset 55 08c70af8af84
parent 53 e7831b781512
child 56 cace0afae0fa
permissions -rw-r--r--
HidDevice consolidating contruction.
sl@25
     1
using System;
sl@25
     2
using System.Windows.Forms;
sl@25
     3
using System.Runtime.InteropServices;
sl@25
     4
using System.Diagnostics;
sl@25
     5
using System.Text;
sl@25
     6
using Microsoft.Win32.SafeHandles;
StephaneLenclud@53
     7
using Win32;
sl@25
     8
sl@25
     9
namespace Hid
sl@25
    10
{
sl@25
    11
    /// <summary>
sl@25
    12
    /// Represent a HID device.
sl@25
    13
    /// </summary>
StephaneLenclud@52
    14
    public class HidDevice: IDisposable
sl@25
    15
    {
sl@25
    16
        public string Name { get; private set; }
sl@25
    17
        public string Manufacturer { get; private set; }
sl@25
    18
        public string Product { get; private set; }
sl@25
    19
        public ushort VendorId { get; private set; }
sl@25
    20
        public ushort ProductId { get; private set; }
sl@25
    21
        public ushort Version { get; private set; }
StephaneLenclud@52
    22
        public IntPtr PreParsedData {get; private set;}
StephaneLenclud@53
    23
        public RID_DEVICE_INFO Info { get {return iInfo;} }
StephaneLenclud@53
    24
        private RID_DEVICE_INFO iInfo;
sl@25
    25
sl@25
    26
        /// <summary>
sl@26
    27
        /// Class constructor will fetch this object properties from HID sub system.
sl@25
    28
        /// </summary>
sl@26
    29
        /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
sl@25
    30
        public HidDevice(IntPtr hRawInputDevice)
sl@25
    31
        {
StephaneLenclud@55
    32
            //Try construct and rollback if needed
StephaneLenclud@55
    33
            try
StephaneLenclud@55
    34
            {
StephaneLenclud@55
    35
                Construct(hRawInputDevice);
StephaneLenclud@55
    36
            }
StephaneLenclud@55
    37
            catch (System.Exception ex)
StephaneLenclud@55
    38
            {
StephaneLenclud@55
    39
                //Just rollback and propagate
StephaneLenclud@55
    40
                Dispose();
StephaneLenclud@55
    41
                throw ex;
StephaneLenclud@55
    42
            }
StephaneLenclud@55
    43
        }
StephaneLenclud@55
    44
StephaneLenclud@55
    45
StephaneLenclud@55
    46
        /// <summary>
StephaneLenclud@55
    47
        /// Make sure dispose is called even if the user forgot about it.
StephaneLenclud@55
    48
        /// </summary>
StephaneLenclud@55
    49
        ~HidDevice()
StephaneLenclud@55
    50
        {
StephaneLenclud@55
    51
            Dispose();
StephaneLenclud@55
    52
        }
StephaneLenclud@55
    53
StephaneLenclud@55
    54
        /// <summary>
StephaneLenclud@55
    55
        /// Private constructor.
StephaneLenclud@55
    56
        /// </summary>
StephaneLenclud@55
    57
        /// <param name="hRawInputDevice"></param>
StephaneLenclud@55
    58
        private void Construct(IntPtr hRawInputDevice)
StephaneLenclud@55
    59
        {
StephaneLenclud@52
    60
            PreParsedData = IntPtr.Zero;
sl@25
    61
            //Fetch various information defining the given HID device
StephaneLenclud@52
    62
            Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
StephaneLenclud@52
    63
StephaneLenclud@52
    64
            //Get our HID descriptor pre-parsed data
StephaneLenclud@52
    65
            PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
StephaneLenclud@53
    66
            if (PreParsedData == IntPtr.Zero)
StephaneLenclud@53
    67
            {
StephaneLenclud@55
    68
                throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@53
    69
            }
StephaneLenclud@53
    70
StephaneLenclud@53
    71
            //Fetch device info
StephaneLenclud@53
    72
            iInfo = new RID_DEVICE_INFO();
StephaneLenclud@53
    73
            if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
StephaneLenclud@53
    74
            {
StephaneLenclud@55
    75
                throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@53
    76
            }
StephaneLenclud@55
    77
sl@25
    78
            //Open our device from the device name/path
StephaneLenclud@55
    79
            SafeFileHandle handle = Win32.Function.CreateFile(Name,
sl@25
    80
                Win32.FileAccess.NONE,
StephaneLenclud@55
    81
                Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
sl@25
    82
                IntPtr.Zero,
sl@25
    83
                Win32.CreationDisposition.OPEN_EXISTING,
sl@25
    84
                Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
sl@25
    85
                IntPtr.Zero
sl@25
    86
                );
sl@25
    87
StephaneLenclud@55
    88
            //Check if CreateFile worked
sl@25
    89
            if (handle.IsInvalid)
sl@25
    90
            {
StephaneLenclud@55
    91
                throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
sl@25
    92
            }
StephaneLenclud@55
    93
StephaneLenclud@55
    94
            //Get manufacturer string
StephaneLenclud@55
    95
            StringBuilder manufacturerString = new StringBuilder(256);
StephaneLenclud@55
    96
            if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
sl@25
    97
            {
StephaneLenclud@55
    98
                Manufacturer = manufacturerString.ToString();
StephaneLenclud@55
    99
            }
sl@25
   100
StephaneLenclud@55
   101
            //Get product string
StephaneLenclud@55
   102
            StringBuilder productString = new StringBuilder(256);
StephaneLenclud@55
   103
            if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
StephaneLenclud@55
   104
            {
StephaneLenclud@55
   105
                Product = productString.ToString();
StephaneLenclud@55
   106
            }
sl@25
   107
StephaneLenclud@55
   108
            //Get attributes
StephaneLenclud@55
   109
            Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
StephaneLenclud@55
   110
            if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
StephaneLenclud@55
   111
            {
StephaneLenclud@55
   112
                VendorId = attributes.VendorID;
StephaneLenclud@55
   113
                ProductId = attributes.ProductID;
StephaneLenclud@55
   114
                Version = attributes.VersionNumber;
StephaneLenclud@55
   115
            }
sl@25
   116
StephaneLenclud@55
   117
            handle.Close();
StephaneLenclud@52
   118
        }
StephaneLenclud@52
   119
StephaneLenclud@52
   120
        /// <summary>
StephaneLenclud@52
   121
        /// Dispose is just for unmanaged clean-up.
StephaneLenclud@52
   122
        /// Make sure calling disposed multiple times does not crash.
StephaneLenclud@52
   123
        /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
StephaneLenclud@52
   124
        /// </summary>
StephaneLenclud@52
   125
        public void Dispose()
StephaneLenclud@52
   126
        {
StephaneLenclud@52
   127
            Marshal.FreeHGlobal(PreParsedData);
StephaneLenclud@52
   128
            PreParsedData = IntPtr.Zero;
StephaneLenclud@52
   129
        }
StephaneLenclud@52
   130
sl@25
   131
        /// <summary>
sl@25
   132
        /// Print information about this device to our debug output.
sl@25
   133
        /// </summary>
sl@25
   134
        public void DebugWrite()
sl@25
   135
        {
sl@25
   136
            Debug.WriteLine("================ HID =========================================================================================");
sl@25
   137
            Debug.WriteLine("==== Name: " + Name);
sl@25
   138
            Debug.WriteLine("==== Manufacturer: " + Manufacturer);
sl@25
   139
            Debug.WriteLine("==== Product: " + Product);
sl@25
   140
            Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
sl@25
   141
            Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
sl@25
   142
            Debug.WriteLine("==== Version: " + Version.ToString());
sl@25
   143
            Debug.WriteLine("==============================================================================================================");
sl@25
   144
        }
sl@25
   145
sl@25
   146
    }
sl@25
   147
sl@25
   148
}