Adding HidDevice class.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
6 using Microsoft.Win32.SafeHandles;
11 /// Represent a HID device.
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; }
25 /// <param name="hRawInputDevice"></param>
26 public HidDevice(IntPtr hRawInputDevice)
28 //Fetch various information defining the given HID device
29 Name = Win32.RawInput.GetDeviceName(hRawInputDevice);
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,
36 Win32.CreationDisposition.OPEN_EXISTING,
37 Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
43 Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
47 //Get manufacturer string
48 StringBuilder manufacturerString = new StringBuilder(256);
49 if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
51 Manufacturer = manufacturerString.ToString();
55 StringBuilder productString = new StringBuilder(256);
56 if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
58 Product = productString.ToString();
62 Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES();
63 if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
65 VendorId = attributes.VendorID;
66 ProductId = attributes.ProductID;
67 Version = attributes.VersionNumber;
75 /// Print information about this device to our debug output.
77 public void DebugWrite()
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("==============================================================================================================");