HidDevice.cs
author StephaneLenclud
Sun, 15 Feb 2015 00:04:28 +0100
changeset 54 7647691aa209
parent 52 2f34ceaf0692
child 55 08c70af8af84
permissions -rw-r--r--
Comments and white space clean up.
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@52
    32
            PreParsedData = IntPtr.Zero;
sl@25
    33
            //Fetch various information defining the given HID device
StephaneLenclud@52
    34
            Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
StephaneLenclud@52
    35
StephaneLenclud@52
    36
            //Get our HID descriptor pre-parsed data
StephaneLenclud@52
    37
            PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
StephaneLenclud@53
    38
            if (PreParsedData == IntPtr.Zero)
StephaneLenclud@53
    39
            {
StephaneLenclud@53
    40
                throw new Exception("HidDevice: GetPreParsedData failed!");
StephaneLenclud@53
    41
            }
StephaneLenclud@53
    42
StephaneLenclud@53
    43
            //Fetch device info
StephaneLenclud@53
    44
            iInfo = new RID_DEVICE_INFO();
StephaneLenclud@53
    45
            if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
StephaneLenclud@53
    46
            {
StephaneLenclud@53
    47
                throw new Exception("HidDevice: GetDeviceInfo failed!");
StephaneLenclud@53
    48
            }
sl@25
    49
                
sl@25
    50
            //Open our device from the device name/path
sl@25
    51
            SafeFileHandle handle=Win32.Function.CreateFile(Name,
sl@25
    52
                Win32.FileAccess.NONE,
sl@25
    53
                Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
sl@25
    54
                IntPtr.Zero,
sl@25
    55
                Win32.CreationDisposition.OPEN_EXISTING,
sl@25
    56
                Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
sl@25
    57
                IntPtr.Zero
sl@25
    58
                );
sl@25
    59
StephaneLenclud@53
    60
            //TODO: should we throw instead?
sl@25
    61
            if (handle.IsInvalid)
sl@25
    62
            {
sl@25
    63
                Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
sl@25
    64
            }
sl@25
    65
            else
sl@25
    66
            {
sl@25
    67
                //Get manufacturer string
sl@25
    68
                StringBuilder manufacturerString = new StringBuilder(256);
sl@25
    69
                if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
sl@25
    70
                {
sl@25
    71
                    Manufacturer = manufacturerString.ToString();                    
sl@25
    72
                }
sl@25
    73
sl@25
    74
                //Get product string
sl@25
    75
                StringBuilder productString = new StringBuilder(256);
sl@25
    76
                if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
sl@25
    77
                {
sl@25
    78
                    Product = productString.ToString();                    
sl@25
    79
                }
sl@25
    80
sl@25
    81
                //Get attributes
sl@25
    82
                Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES();
sl@25
    83
                if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
sl@25
    84
                {
sl@25
    85
                    VendorId = attributes.VendorID;
sl@25
    86
                    ProductId = attributes.ProductID;
sl@25
    87
                    Version = attributes.VersionNumber;
sl@25
    88
                }
sl@25
    89
sl@25
    90
                handle.Close();
sl@25
    91
            }
sl@25
    92
        }
sl@25
    93
StephaneLenclud@52
    94
StephaneLenclud@52
    95
        /// <summary>
StephaneLenclud@52
    96
        /// Make sure dispose is called even if the user forgot about it.
StephaneLenclud@52
    97
        /// </summary>
StephaneLenclud@52
    98
        ~HidDevice()
StephaneLenclud@52
    99
        {
StephaneLenclud@52
   100
            Dispose();
StephaneLenclud@52
   101
        }
StephaneLenclud@52
   102
StephaneLenclud@52
   103
        /// <summary>
StephaneLenclud@52
   104
        /// Dispose is just for unmanaged clean-up.
StephaneLenclud@52
   105
        /// Make sure calling disposed multiple times does not crash.
StephaneLenclud@52
   106
        /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
StephaneLenclud@52
   107
        /// </summary>
StephaneLenclud@52
   108
        public void Dispose()
StephaneLenclud@52
   109
        {
StephaneLenclud@52
   110
            Marshal.FreeHGlobal(PreParsedData);
StephaneLenclud@52
   111
            PreParsedData = IntPtr.Zero;
StephaneLenclud@52
   112
        }
StephaneLenclud@52
   113
sl@25
   114
        /// <summary>
sl@25
   115
        /// Print information about this device to our debug output.
sl@25
   116
        /// </summary>
sl@25
   117
        public void DebugWrite()
sl@25
   118
        {
sl@25
   119
            Debug.WriteLine("================ HID =========================================================================================");
sl@25
   120
            Debug.WriteLine("==== Name: " + Name);
sl@25
   121
            Debug.WriteLine("==== Manufacturer: " + Manufacturer);
sl@25
   122
            Debug.WriteLine("==== Product: " + Product);
sl@25
   123
            Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
sl@25
   124
            Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
sl@25
   125
            Debug.WriteLine("==== Version: " + Version.ToString());
sl@25
   126
            Debug.WriteLine("==============================================================================================================");
sl@25
   127
        }
sl@25
   128
sl@25
   129
    }
sl@25
   130
sl@25
   131
}