1.1 --- a/HidDevice.cs Sun Feb 15 00:04:28 2015 +0100
1.2 +++ b/HidDevice.cs Sun Feb 15 10:50:09 2015 +0100
1.3 @@ -29,6 +29,34 @@
1.4 /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
1.5 public HidDevice(IntPtr hRawInputDevice)
1.6 {
1.7 + //Try construct and rollback if needed
1.8 + try
1.9 + {
1.10 + Construct(hRawInputDevice);
1.11 + }
1.12 + catch (System.Exception ex)
1.13 + {
1.14 + //Just rollback and propagate
1.15 + Dispose();
1.16 + throw ex;
1.17 + }
1.18 + }
1.19 +
1.20 +
1.21 + /// <summary>
1.22 + /// Make sure dispose is called even if the user forgot about it.
1.23 + /// </summary>
1.24 + ~HidDevice()
1.25 + {
1.26 + Dispose();
1.27 + }
1.28 +
1.29 + /// <summary>
1.30 + /// Private constructor.
1.31 + /// </summary>
1.32 + /// <param name="hRawInputDevice"></param>
1.33 + private void Construct(IntPtr hRawInputDevice)
1.34 + {
1.35 PreParsedData = IntPtr.Zero;
1.36 //Fetch various information defining the given HID device
1.37 Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
1.38 @@ -37,67 +65,56 @@
1.39 PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
1.40 if (PreParsedData == IntPtr.Zero)
1.41 {
1.42 - throw new Exception("HidDevice: GetPreParsedData failed!");
1.43 + throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString());
1.44 }
1.45
1.46 //Fetch device info
1.47 iInfo = new RID_DEVICE_INFO();
1.48 if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
1.49 {
1.50 - throw new Exception("HidDevice: GetDeviceInfo failed!");
1.51 + throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
1.52 }
1.53 -
1.54 +
1.55 //Open our device from the device name/path
1.56 - SafeFileHandle handle=Win32.Function.CreateFile(Name,
1.57 + SafeFileHandle handle = Win32.Function.CreateFile(Name,
1.58 Win32.FileAccess.NONE,
1.59 - Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
1.60 + Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
1.61 IntPtr.Zero,
1.62 Win32.CreationDisposition.OPEN_EXISTING,
1.63 Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
1.64 IntPtr.Zero
1.65 );
1.66
1.67 - //TODO: should we throw instead?
1.68 + //Check if CreateFile worked
1.69 if (handle.IsInvalid)
1.70 {
1.71 - Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
1.72 + throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
1.73 }
1.74 - else
1.75 +
1.76 + //Get manufacturer string
1.77 + StringBuilder manufacturerString = new StringBuilder(256);
1.78 + if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
1.79 {
1.80 - //Get manufacturer string
1.81 - StringBuilder manufacturerString = new StringBuilder(256);
1.82 - if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
1.83 - {
1.84 - Manufacturer = manufacturerString.ToString();
1.85 - }
1.86 + Manufacturer = manufacturerString.ToString();
1.87 + }
1.88
1.89 - //Get product string
1.90 - StringBuilder productString = new StringBuilder(256);
1.91 - if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
1.92 - {
1.93 - Product = productString.ToString();
1.94 - }
1.95 + //Get product string
1.96 + StringBuilder productString = new StringBuilder(256);
1.97 + if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
1.98 + {
1.99 + Product = productString.ToString();
1.100 + }
1.101
1.102 - //Get attributes
1.103 - Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES();
1.104 - if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
1.105 - {
1.106 - VendorId = attributes.VendorID;
1.107 - ProductId = attributes.ProductID;
1.108 - Version = attributes.VersionNumber;
1.109 - }
1.110 + //Get attributes
1.111 + Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
1.112 + if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
1.113 + {
1.114 + VendorId = attributes.VendorID;
1.115 + ProductId = attributes.ProductID;
1.116 + Version = attributes.VersionNumber;
1.117 + }
1.118
1.119 - handle.Close();
1.120 - }
1.121 - }
1.122 -
1.123 -
1.124 - /// <summary>
1.125 - /// Make sure dispose is called even if the user forgot about it.
1.126 - /// </summary>
1.127 - ~HidDevice()
1.128 - {
1.129 - Dispose();
1.130 + handle.Close();
1.131 }
1.132
1.133 /// <summary>