Much improved raw input device list.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
6 using Microsoft.Win32.SafeHandles;
12 /// Represent a HID device.
13 /// Rename to RawInputDevice?
15 public class HidDevice: IDisposable
17 public string Name { get; private set; }
18 public string Manufacturer { get; private set; }
19 public string Product { get; private set; }
20 public ushort VendorId { get; private set; }
21 public ushort ProductId { get; private set; }
22 public ushort Version { get; private set; }
23 //Pre-parsed HID descriptor
24 public IntPtr PreParsedData {get; private set;}
26 public RID_DEVICE_INFO Info { get {return iInfo;} }
27 private RID_DEVICE_INFO iInfo;
29 public HIDP_CAPS Capabilities { get { return iCapabilities; } }
30 private HIDP_CAPS iCapabilities;
31 //Input Button Capabilities
32 public HIDP_BUTTON_CAPS[] InputButtonCapabilities { get { return iInputButtonCapabilities; } }
33 private HIDP_BUTTON_CAPS[] iInputButtonCapabilities;
34 //Input Value Capabilities
35 public HIDP_VALUE_CAPS[] InputValueCapabilities { get { return iInputValueCapabilities; } }
36 private HIDP_VALUE_CAPS[] iInputValueCapabilities;
42 /// Class constructor will fetch this object properties from HID sub system.
44 /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
45 public HidDevice(IntPtr hRawInputDevice)
47 //Try construct and rollback if needed
50 Construct(hRawInputDevice);
52 catch (System.Exception ex)
54 //Just rollback and propagate
62 /// Make sure dispose is called even if the user forgot about it.
70 /// Private constructor.
72 /// <param name="hRawInputDevice"></param>
73 private void Construct(IntPtr hRawInputDevice)
75 PreParsedData = IntPtr.Zero;
76 iInputButtonCapabilities = null;
77 iInputValueCapabilities = null;
79 //Fetch various information defining the given HID device
80 Name = Win32.RawInput.GetDeviceName(hRawInputDevice);
83 iInfo = new RID_DEVICE_INFO();
84 if (!Win32.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
86 throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
89 //Open our device from the device name/path
90 SafeFileHandle handle = Win32.Function.CreateFile(Name,
91 Win32.FileAccess.NONE,
92 Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
94 Win32.CreationDisposition.OPEN_EXISTING,
95 Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
99 //Check if CreateFile worked
100 if (handle.IsInvalid)
102 throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
105 //Get manufacturer string
106 StringBuilder manufacturerString = new StringBuilder(256);
107 if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
109 Manufacturer = manufacturerString.ToString();
113 StringBuilder productString = new StringBuilder(256);
114 if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
116 Product = productString.ToString();
120 Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
121 if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
123 VendorId = attributes.VendorID;
124 ProductId = attributes.ProductID;
125 Version = attributes.VersionNumber;
130 //Get our HID descriptor pre-parsed data
131 PreParsedData = Win32.RawInput.GetPreParsedData(hRawInputDevice);
133 if (PreParsedData == IntPtr.Zero)
136 //Some devices don't have pre-parsed data.
141 HidStatus status = Win32.Function.HidP_GetCaps(PreParsedData, ref iCapabilities);
142 if (status != HidStatus.HIDP_STATUS_SUCCESS)
144 throw new Exception("HidDevice: HidP_GetCaps failed: " + status.ToString());
147 //Get input button caps if needed
148 if (Capabilities.NumberInputButtonCaps > 0)
150 iInputButtonCapabilities = new HIDP_BUTTON_CAPS[Capabilities.NumberInputButtonCaps];
151 ushort buttonCapabilitiesLength = Capabilities.NumberInputButtonCaps;
152 status = Win32.Function.HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, iInputButtonCapabilities, ref buttonCapabilitiesLength, PreParsedData);
153 if (status != HidStatus.HIDP_STATUS_SUCCESS || buttonCapabilitiesLength != Capabilities.NumberInputButtonCaps)
155 throw new Exception("HidDevice: HidP_GetButtonCaps failed: " + status.ToString());
159 //Get input value caps if needed
160 if (Capabilities.NumberInputValueCaps > 0)
162 iInputValueCapabilities = new HIDP_VALUE_CAPS[Capabilities.NumberInputValueCaps];
163 ushort valueCapabilitiesLength = Capabilities.NumberInputValueCaps;
164 status = Win32.Function.HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, iInputValueCapabilities, ref valueCapabilitiesLength, PreParsedData);
165 if (status != HidStatus.HIDP_STATUS_SUCCESS || valueCapabilitiesLength != Capabilities.NumberInputValueCaps)
167 throw new Exception("HidDevice: HidP_GetValueCaps failed: " + status.ToString());
175 /// Dispose is just for unmanaged clean-up.
176 /// Make sure calling disposed multiple times does not crash.
177 /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
179 public void Dispose()
181 Marshal.FreeHGlobal(PreParsedData);
182 PreParsedData = IntPtr.Zero;
186 /// Print information about this device to our debug output.
188 public void DebugWrite()
190 Debug.WriteLine("================ HID =========================================================================================");
191 Debug.WriteLine("==== Name: " + Name);
192 Debug.WriteLine("==== Manufacturer: " + Manufacturer);
193 Debug.WriteLine("==== Product: " + Product);
194 Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
195 Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
196 Debug.WriteLine("==== Version: " + Version.ToString());
197 Debug.WriteLine("==============================================================================================================");