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