HidUtil.cs
changeset 29 7679a5ab194b
parent 28 6af1cbb3beb4
child 30 8ce9bdc9c5b5
     1.1 --- a/HidUtil.cs	Sat Dec 06 22:59:55 2014 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,94 +0,0 @@
     1.4 -using System;
     1.5 -using System.Windows.Forms;
     1.6 -using System.Runtime.InteropServices;
     1.7 -using System.Diagnostics;
     1.8 -using System.Text;
     1.9 -using Microsoft.Win32.SafeHandles;
    1.10 -
    1.11 -namespace Hid
    1.12 -{
    1.13 -    /// <summary>
    1.14 -    /// Represent a HID device.
    1.15 -    /// </summary>
    1.16 -    class HidDevice
    1.17 -    {
    1.18 -        public string Name { get; private set; }
    1.19 -        public string Manufacturer { get; private set; }
    1.20 -        public string Product { get; private set; }
    1.21 -        public ushort VendorId { get; private set; }
    1.22 -        public ushort ProductId { get; private set; }
    1.23 -        public ushort Version { get; private set; }
    1.24 -
    1.25 -        /// <summary>
    1.26 -        /// Class constructor will fetch this object properties from HID sub system.
    1.27 -        /// </summary>
    1.28 -        /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
    1.29 -        public HidDevice(IntPtr hRawInputDevice)
    1.30 -        {
    1.31 -            //Fetch various information defining the given HID device
    1.32 -            Name = Win32.RawInput.GetDeviceName(hRawInputDevice);            
    1.33 -                
    1.34 -            //Open our device from the device name/path
    1.35 -            SafeFileHandle handle=Win32.Function.CreateFile(Name,
    1.36 -                Win32.FileAccess.NONE,
    1.37 -                Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
    1.38 -                IntPtr.Zero,
    1.39 -                Win32.CreationDisposition.OPEN_EXISTING,
    1.40 -                Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
    1.41 -                IntPtr.Zero
    1.42 -                );
    1.43 -
    1.44 -            if (handle.IsInvalid)
    1.45 -            {
    1.46 -                Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
    1.47 -            }
    1.48 -            else
    1.49 -            {
    1.50 -                //Get manufacturer string
    1.51 -                StringBuilder manufacturerString = new StringBuilder(256);
    1.52 -                if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
    1.53 -                {
    1.54 -                    Manufacturer = manufacturerString.ToString();                    
    1.55 -                }
    1.56 -
    1.57 -                //Get product string
    1.58 -                StringBuilder productString = new StringBuilder(256);
    1.59 -                if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
    1.60 -                {
    1.61 -                    Product = productString.ToString();                    
    1.62 -                }
    1.63 -
    1.64 -                //Get attributes
    1.65 -                Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES();
    1.66 -                if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
    1.67 -                {
    1.68 -                    VendorId = attributes.VendorID;
    1.69 -                    ProductId = attributes.ProductID;
    1.70 -                    Version = attributes.VersionNumber;
    1.71 -                }
    1.72 -
    1.73 -                handle.Close();
    1.74 -            }
    1.75 -        }
    1.76 -
    1.77 -        /// <summary>
    1.78 -        /// Print information about this device to our debug output.
    1.79 -        /// </summary>
    1.80 -        public void DebugWrite()
    1.81 -        {
    1.82 -            Debug.WriteLine("================ HID =========================================================================================");
    1.83 -            Debug.WriteLine("==== Name: " + Name);
    1.84 -            Debug.WriteLine("==== Manufacturer: " + Manufacturer);
    1.85 -            Debug.WriteLine("==== Product: " + Product);
    1.86 -            Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
    1.87 -            Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
    1.88 -            Debug.WriteLine("==== Version: " + Version.ToString());
    1.89 -            Debug.WriteLine("==============================================================================================================");
    1.90 -        }
    1.91 -
    1.92 -
    1.93 -
    1.94 -
    1.95 -    }
    1.96 -
    1.97 -}
    1.98 \ No newline at end of file