Adding HidDevice class.
authorsl
Sat, 06 Dec 2014 13:08:50 +0100
changeset 255f4e0fbb3ea1
parent 24 974f5fdaebfb
child 26 0166dfab3349
Adding HidDevice class.
HidUtil.cs
HumanInterfaceDevice.cs
RemoteControlDevice.cs
RemoteControlSample.csproj
Win32Hid.cs
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/HidUtil.cs	Sat Dec 06 13:08:50 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     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 +        /// 
    1.27 +        /// </summary>
    1.28 +        /// <param name="hRawInputDevice"></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
     2.1 --- a/HumanInterfaceDevice.cs	Sat Dec 06 12:13:39 2014 +0100
     2.2 +++ b/HumanInterfaceDevice.cs	Sat Dec 06 13:08:50 2014 +0100
     2.3 @@ -389,7 +389,6 @@
     2.4              AppCtrlTileVertically = 0x023B,
     2.5              AppCtrlFormat = 0x023C,
     2.6              AppCtrlEdit = 0x023D,
     2.7 -
     2.8          }
     2.9      }
    2.10  }
    2.11 \ No newline at end of file
     3.1 --- a/RemoteControlDevice.cs	Sat Dec 06 12:13:39 2014 +0100
     3.2 +++ b/RemoteControlDevice.cs	Sat Dec 06 13:08:50 2014 +0100
     3.3 @@ -385,7 +385,7 @@
     3.4  
     3.5              //Declare some pointers
     3.6              IntPtr rawInputBuffer = IntPtr.Zero;
     3.7 -            //My understanding is that this is basically our HID descriptor
     3.8 +            //My understanding is that this is basically our HID descriptor 
     3.9              IntPtr preParsedData = IntPtr.Zero;
    3.10  
    3.11              try
    3.12 @@ -406,45 +406,10 @@
    3.13                      return;
    3.14                  }
    3.15  
    3.16 -                //Debug
    3.17 -                string deviceName = RawInput.GetDeviceName(rawInput.header.hDevice);
    3.18 -                Debug.WriteLine("Device name: " + deviceName);
    3.19 +                //Get various information about this HID device
    3.20 +                Hid.HidDevice device = new Hid.HidDevice(rawInput.header.hDevice);
    3.21 +                device.DebugWrite();
    3.22                  
    3.23 -                //Open our device from the device name/path
    3.24 -                SafeFileHandle handle=Win32.Function.CreateFile(deviceName,
    3.25 -                    Win32.FileAccess.NONE,
    3.26 -                    Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
    3.27 -                    IntPtr.Zero,
    3.28 -                    Win32.CreationDisposition.OPEN_EXISTING,
    3.29 -                    Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
    3.30 -                    IntPtr.Zero
    3.31 -                    );
    3.32 -
    3.33 -                if (handle.IsInvalid)
    3.34 -                {
    3.35 -                    Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
    3.36 -                }
    3.37 -                else
    3.38 -                {
    3.39 -                    //Get manufacturer string
    3.40 -                    StringBuilder manufacturerString = new StringBuilder(256);
    3.41 -                    bool returnStatus = Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity);
    3.42 -                    if (returnStatus)
    3.43 -                    {
    3.44 -                        Debug.WriteLine("Manufacturer: " + manufacturerString.ToString());
    3.45 -                    }
    3.46 -
    3.47 -                    //Get product string
    3.48 -                    StringBuilder productString = new StringBuilder(256);
    3.49 -                    returnStatus = Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity);
    3.50 -                    if (returnStatus)
    3.51 -                    {
    3.52 -                        Debug.WriteLine("Product: " + productString.ToString());
    3.53 -                    }
    3.54 -
    3.55 -                    handle.Close();
    3.56 -
    3.57 -                }
    3.58                 
    3.59  
    3.60  
     4.1 --- a/RemoteControlSample.csproj	Sat Dec 06 12:13:39 2014 +0100
     4.2 +++ b/RemoteControlSample.csproj	Sat Dec 06 13:08:50 2014 +0100
     4.3 @@ -127,6 +127,7 @@
     4.4      <Compile Include="Form1.cs">
     4.5        <SubType>Form</SubType>
     4.6      </Compile>
     4.7 +    <Compile Include="HidUtil.cs" />
     4.8      <Compile Include="HumanInterfaceDevice.cs" />
     4.9      <Compile Include="RawInput.cs" />
    4.10      <Compile Include="RemoteControlDevice.cs">
     5.1 --- a/Win32Hid.cs	Sat Dec 06 12:13:39 2014 +0100
     5.2 +++ b/Win32Hid.cs	Sat Dec 06 13:08:50 2014 +0100
     5.3 @@ -17,6 +17,8 @@
     5.4          [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
     5.5          public static extern Boolean HidD_GetProductString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
     5.6  
     5.7 +        [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
     5.8 +        public static extern Boolean HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
     5.9      }
    5.10  
    5.11  
    5.12 @@ -74,5 +76,14 @@
    5.13          public ushort UsagePage;
    5.14      };
    5.15  
    5.16 +    [StructLayout(LayoutKind.Sequential)]
    5.17 +    public struct HIDD_ATTRIBUTES
    5.18 +    {
    5.19 +        public uint Size;
    5.20 +        public ushort VendorID;
    5.21 +        public ushort ProductID;
    5.22 +        public ushort VersionNumber;
    5.23 +    }
    5.24 +
    5.25  
    5.26  }
    5.27 \ No newline at end of file