# HG changeset patch # User sl # Date 1417867730 -3600 # Node ID 5f4e0fbb3ea1670b918bbc166a4449687f8a3f53 # Parent 974f5fdaebfbb366a7f87b3cf44d09b7fee5e529 Adding HidDevice class. diff -r 974f5fdaebfb -r 5f4e0fbb3ea1 HidUtil.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HidUtil.cs Sat Dec 06 13:08:50 2014 +0100 @@ -0,0 +1,94 @@ +using System; +using System.Windows.Forms; +using System.Runtime.InteropServices; +using System.Diagnostics; +using System.Text; +using Microsoft.Win32.SafeHandles; + +namespace Hid +{ + /// + /// Represent a HID device. + /// + class HidDevice + { + public string Name { get; private set; } + public string Manufacturer { get; private set; } + public string Product { get; private set; } + public ushort VendorId { get; private set; } + public ushort ProductId { get; private set; } + public ushort Version { get; private set; } + + /// + /// + /// + /// + public HidDevice(IntPtr hRawInputDevice) + { + //Fetch various information defining the given HID device + Name = Win32.RawInput.GetDeviceName(hRawInputDevice); + + //Open our device from the device name/path + SafeFileHandle handle=Win32.Function.CreateFile(Name, + Win32.FileAccess.NONE, + Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE, + IntPtr.Zero, + Win32.CreationDisposition.OPEN_EXISTING, + Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED, + IntPtr.Zero + ); + + if (handle.IsInvalid) + { + Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString()); + } + else + { + //Get manufacturer string + StringBuilder manufacturerString = new StringBuilder(256); + if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity)) + { + Manufacturer = manufacturerString.ToString(); + } + + //Get product string + StringBuilder productString = new StringBuilder(256); + if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity)) + { + Product = productString.ToString(); + } + + //Get attributes + Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES(); + if (Win32.Function.HidD_GetAttributes(handle, ref attributes)) + { + VendorId = attributes.VendorID; + ProductId = attributes.ProductID; + Version = attributes.VersionNumber; + } + + handle.Close(); + } + } + + /// + /// Print information about this device to our debug output. + /// + public void DebugWrite() + { + Debug.WriteLine("================ HID ========================================================================================="); + Debug.WriteLine("==== Name: " + Name); + Debug.WriteLine("==== Manufacturer: " + Manufacturer); + Debug.WriteLine("==== Product: " + Product); + Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4")); + Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4")); + Debug.WriteLine("==== Version: " + Version.ToString()); + Debug.WriteLine("=============================================================================================================="); + } + + + + + } + +} \ No newline at end of file diff -r 974f5fdaebfb -r 5f4e0fbb3ea1 HumanInterfaceDevice.cs --- a/HumanInterfaceDevice.cs Sat Dec 06 12:13:39 2014 +0100 +++ b/HumanInterfaceDevice.cs Sat Dec 06 13:08:50 2014 +0100 @@ -389,7 +389,6 @@ AppCtrlTileVertically = 0x023B, AppCtrlFormat = 0x023C, AppCtrlEdit = 0x023D, - } } } \ No newline at end of file diff -r 974f5fdaebfb -r 5f4e0fbb3ea1 RemoteControlDevice.cs --- a/RemoteControlDevice.cs Sat Dec 06 12:13:39 2014 +0100 +++ b/RemoteControlDevice.cs Sat Dec 06 13:08:50 2014 +0100 @@ -385,7 +385,7 @@ //Declare some pointers IntPtr rawInputBuffer = IntPtr.Zero; - //My understanding is that this is basically our HID descriptor + //My understanding is that this is basically our HID descriptor IntPtr preParsedData = IntPtr.Zero; try @@ -406,45 +406,10 @@ return; } - //Debug - string deviceName = RawInput.GetDeviceName(rawInput.header.hDevice); - Debug.WriteLine("Device name: " + deviceName); + //Get various information about this HID device + Hid.HidDevice device = new Hid.HidDevice(rawInput.header.hDevice); + device.DebugWrite(); - //Open our device from the device name/path - SafeFileHandle handle=Win32.Function.CreateFile(deviceName, - Win32.FileAccess.NONE, - Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE, - IntPtr.Zero, - Win32.CreationDisposition.OPEN_EXISTING, - Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED, - IntPtr.Zero - ); - - if (handle.IsInvalid) - { - Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString()); - } - else - { - //Get manufacturer string - StringBuilder manufacturerString = new StringBuilder(256); - bool returnStatus = Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity); - if (returnStatus) - { - Debug.WriteLine("Manufacturer: " + manufacturerString.ToString()); - } - - //Get product string - StringBuilder productString = new StringBuilder(256); - returnStatus = Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity); - if (returnStatus) - { - Debug.WriteLine("Product: " + productString.ToString()); - } - - handle.Close(); - - } diff -r 974f5fdaebfb -r 5f4e0fbb3ea1 RemoteControlSample.csproj --- a/RemoteControlSample.csproj Sat Dec 06 12:13:39 2014 +0100 +++ b/RemoteControlSample.csproj Sat Dec 06 13:08:50 2014 +0100 @@ -127,6 +127,7 @@ Form + diff -r 974f5fdaebfb -r 5f4e0fbb3ea1 Win32Hid.cs --- a/Win32Hid.cs Sat Dec 06 12:13:39 2014 +0100 +++ b/Win32Hid.cs Sat Dec 06 13:08:50 2014 +0100 @@ -17,6 +17,8 @@ [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern Boolean HidD_GetProductString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength); + [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern Boolean HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes); } @@ -74,5 +76,14 @@ public ushort UsagePage; }; + [StructLayout(LayoutKind.Sequential)] + public struct HIDD_ATTRIBUTES + { + public uint Size; + public ushort VendorID; + public ushort ProductID; + public ushort VersionNumber; + } + } \ No newline at end of file