HidDevice.cs
author sl
Mon, 29 Dec 2014 11:45:33 +0100
changeset 47 bd068db7779a
parent 40 b3e177062849
child 52 2f34ceaf0692
permissions -rw-r--r--
Consolidation and adding rebracer settings.
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;
sl@25
     7
sl@25
     8
namespace Hid
sl@25
     9
{
sl@25
    10
    /// <summary>
sl@25
    11
    /// Represent a HID device.
sl@25
    12
    /// </summary>
sl@40
    13
    public class HidDevice
sl@25
    14
    {
sl@25
    15
        public string Name { get; private set; }
sl@25
    16
        public string Manufacturer { get; private set; }
sl@25
    17
        public string Product { get; private set; }
sl@25
    18
        public ushort VendorId { get; private set; }
sl@25
    19
        public ushort ProductId { get; private set; }
sl@25
    20
        public ushort Version { get; private set; }
sl@25
    21
sl@25
    22
        /// <summary>
sl@26
    23
        /// Class constructor will fetch this object properties from HID sub system.
sl@25
    24
        /// </summary>
sl@26
    25
        /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
sl@25
    26
        public HidDevice(IntPtr hRawInputDevice)
sl@25
    27
        {
sl@25
    28
            //Fetch various information defining the given HID device
sl@47
    29
            Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);            
sl@25
    30
                
sl@25
    31
            //Open our device from the device name/path
sl@25
    32
            SafeFileHandle handle=Win32.Function.CreateFile(Name,
sl@25
    33
                Win32.FileAccess.NONE,
sl@25
    34
                Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
sl@25
    35
                IntPtr.Zero,
sl@25
    36
                Win32.CreationDisposition.OPEN_EXISTING,
sl@25
    37
                Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
sl@25
    38
                IntPtr.Zero
sl@25
    39
                );
sl@25
    40
sl@25
    41
            if (handle.IsInvalid)
sl@25
    42
            {
sl@25
    43
                Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
sl@25
    44
            }
sl@25
    45
            else
sl@25
    46
            {
sl@25
    47
                //Get manufacturer string
sl@25
    48
                StringBuilder manufacturerString = new StringBuilder(256);
sl@25
    49
                if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
sl@25
    50
                {
sl@25
    51
                    Manufacturer = manufacturerString.ToString();                    
sl@25
    52
                }
sl@25
    53
sl@25
    54
                //Get product string
sl@25
    55
                StringBuilder productString = new StringBuilder(256);
sl@25
    56
                if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
sl@25
    57
                {
sl@25
    58
                    Product = productString.ToString();                    
sl@25
    59
                }
sl@25
    60
sl@25
    61
                //Get attributes
sl@25
    62
                Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES();
sl@25
    63
                if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
sl@25
    64
                {
sl@25
    65
                    VendorId = attributes.VendorID;
sl@25
    66
                    ProductId = attributes.ProductID;
sl@25
    67
                    Version = attributes.VersionNumber;
sl@25
    68
                }
sl@25
    69
sl@25
    70
                handle.Close();
sl@25
    71
            }
sl@25
    72
        }
sl@25
    73
sl@25
    74
        /// <summary>
sl@25
    75
        /// Print information about this device to our debug output.
sl@25
    76
        /// </summary>
sl@25
    77
        public void DebugWrite()
sl@25
    78
        {
sl@25
    79
            Debug.WriteLine("================ HID =========================================================================================");
sl@25
    80
            Debug.WriteLine("==== Name: " + Name);
sl@25
    81
            Debug.WriteLine("==== Manufacturer: " + Manufacturer);
sl@25
    82
            Debug.WriteLine("==== Product: " + Product);
sl@25
    83
            Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
sl@25
    84
            Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
sl@25
    85
            Debug.WriteLine("==== Version: " + Version.ToString());
sl@25
    86
            Debug.WriteLine("==============================================================================================================");
sl@25
    87
        }
sl@25
    88
sl@25
    89
sl@25
    90
sl@25
    91
sl@25
    92
    }
sl@25
    93
sl@25
    94
}