# HG changeset patch
# User StephaneLenclud
# Date 1423993809 -3600
# Node ID 08c70af8af84956d23bee33f1cd3738aee08abc4
# Parent 7647691aa2090ba719fca3f7abfe6c54aed8efd8
HidDevice consolidating contruction.
diff -r 7647691aa209 -r 08c70af8af84 HidDevice.cs
--- a/HidDevice.cs Sun Feb 15 00:04:28 2015 +0100
+++ b/HidDevice.cs Sun Feb 15 10:50:09 2015 +0100
@@ -29,6 +29,34 @@
/// Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice
public HidDevice(IntPtr hRawInputDevice)
{
+ //Try construct and rollback if needed
+ try
+ {
+ Construct(hRawInputDevice);
+ }
+ catch (System.Exception ex)
+ {
+ //Just rollback and propagate
+ Dispose();
+ throw ex;
+ }
+ }
+
+
+ ///
+ /// Make sure dispose is called even if the user forgot about it.
+ ///
+ ~HidDevice()
+ {
+ Dispose();
+ }
+
+ ///
+ /// Private constructor.
+ ///
+ ///
+ private void Construct(IntPtr hRawInputDevice)
+ {
PreParsedData = IntPtr.Zero;
//Fetch various information defining the given HID device
Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
@@ -37,67 +65,56 @@
PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
if (PreParsedData == IntPtr.Zero)
{
- throw new Exception("HidDevice: GetPreParsedData failed!");
+ throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString());
}
//Fetch device info
iInfo = new RID_DEVICE_INFO();
if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
{
- throw new Exception("HidDevice: GetDeviceInfo failed!");
+ throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
}
-
+
//Open our device from the device name/path
- SafeFileHandle handle=Win32.Function.CreateFile(Name,
+ SafeFileHandle handle = Win32.Function.CreateFile(Name,
Win32.FileAccess.NONE,
- Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
+ Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
IntPtr.Zero,
Win32.CreationDisposition.OPEN_EXISTING,
Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
IntPtr.Zero
);
- //TODO: should we throw instead?
+ //Check if CreateFile worked
if (handle.IsInvalid)
{
- Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
+ throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
}
- else
+
+ //Get manufacturer string
+ StringBuilder manufacturerString = new StringBuilder(256);
+ if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
{
- //Get manufacturer string
- StringBuilder manufacturerString = new StringBuilder(256);
- if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
- {
- Manufacturer = manufacturerString.ToString();
- }
+ Manufacturer = manufacturerString.ToString();
+ }
- //Get product string
- StringBuilder productString = new StringBuilder(256);
- if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
- {
- Product = productString.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;
- }
+ //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();
- }
- }
-
-
- ///
- /// Make sure dispose is called even if the user forgot about it.
- ///
- ~HidDevice()
- {
- Dispose();
+ handle.Close();
}
///