HidDevice now supports capabilities and button capabilities.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
6 using Microsoft.Win32.SafeHandles;
12 /// Represent a HID device.
14 public class HidDevice: IDisposable
16 public string Name { get; private set; }
17 public string Manufacturer { get; private set; }
18 public string Product { get; private set; }
19 public ushort VendorId { get; private set; }
20 public ushort ProductId { get; private set; }
21 public ushort Version { get; private set; }
22 //Pre-parsed HID descriptor
23 public IntPtr PreParsedData {get; private set;}
25 public RID_DEVICE_INFO Info { get {return iInfo;} }
26 private RID_DEVICE_INFO iInfo;
28 public HIDP_CAPS Capabilities { get { return iCapabilities; } }
29 private HIDP_CAPS iCapabilities;
30 //Input Button Capabilities
31 public HIDP_BUTTON_CAPS[] InputButtonCapabilities { get { return iInputButtonCapabilities; } }
32 private HIDP_BUTTON_CAPS[] iInputButtonCapabilities;
36 /// Class constructor will fetch this object properties from HID sub system.
38 /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
39 public HidDevice(IntPtr hRawInputDevice)
41 //Try construct and rollback if needed
44 Construct(hRawInputDevice);
46 catch (System.Exception ex)
48 //Just rollback and propagate
56 /// Make sure dispose is called even if the user forgot about it.
64 /// Private constructor.
66 /// <param name="hRawInputDevice"></param>
67 private void Construct(IntPtr hRawInputDevice)
69 PreParsedData = IntPtr.Zero;
70 //Fetch various information defining the given HID device
71 Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
73 //Get our HID descriptor pre-parsed data
74 PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
75 if (PreParsedData == IntPtr.Zero)
77 throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString());
81 iInfo = new RID_DEVICE_INFO();
82 if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
84 throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
87 //Open our device from the device name/path
88 SafeFileHandle handle = Win32.Function.CreateFile(Name,
89 Win32.FileAccess.NONE,
90 Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
92 Win32.CreationDisposition.OPEN_EXISTING,
93 Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
97 //Check if CreateFile worked
100 throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
103 //Get manufacturer string
104 StringBuilder manufacturerString = new StringBuilder(256);
105 if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
107 Manufacturer = manufacturerString.ToString();
111 StringBuilder productString = new StringBuilder(256);
112 if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
114 Product = productString.ToString();
118 Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
119 if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
121 VendorId = attributes.VendorID;
122 ProductId = attributes.ProductID;
123 Version = attributes.VersionNumber;
129 HidStatus status = Win32.Function.HidP_GetCaps(PreParsedData, ref iCapabilities);
130 if (status != HidStatus.HIDP_STATUS_SUCCESS)
132 throw new Exception("HidDevice: HidP_GetCaps failed: " + status.ToString());
135 //Get input button caps
136 iInputButtonCapabilities = new HIDP_BUTTON_CAPS[Capabilities.NumberInputButtonCaps];
137 ushort buttonCapabilitiesLength = Capabilities.NumberInputButtonCaps;
138 status = Win32.Function.HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, iInputButtonCapabilities, ref buttonCapabilitiesLength, PreParsedData);
139 if (status != HidStatus.HIDP_STATUS_SUCCESS || buttonCapabilitiesLength != Capabilities.NumberInputButtonCaps)
141 throw new Exception("HidDevice: HidP_GetButtonCaps failed: " + status.ToString());
148 /// Dispose is just for unmanaged clean-up.
149 /// Make sure calling disposed multiple times does not crash.
150 /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
152 public void Dispose()
154 Marshal.FreeHGlobal(PreParsedData);
155 PreParsedData = IntPtr.Zero;
159 /// Print information about this device to our debug output.
161 public void DebugWrite()
163 Debug.WriteLine("================ HID =========================================================================================");
164 Debug.WriteLine("==== Name: " + Name);
165 Debug.WriteLine("==== Manufacturer: " + Manufacturer);
166 Debug.WriteLine("==== Product: " + Product);
167 Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
168 Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
169 Debug.WriteLine("==== Version: " + Version.ToString());
170 Debug.WriteLine("==============================================================================================================");