internal static HidDevice Open(string devicePath)
{
IntPtr handle = Kernel32.CreateFile(
devicePath,
(uint)FileAccess.Read,
FileShare.ReadWrite,
IntPtr.Zero,
FileMode.Open,
FileAttributes.Device,
IntPtr.Zero);
if (handle != Kernel32.InvalidHandleValue) {
Hid.DeviceAttributes attributes;
if (Hid.HidD_GetAttributes(handle, out attributes)) {
return new HidDevice(
handle,
devicePath,
attributes.VendorId,
attributes.ProductId,
attributes.VersionNumber);
}
// If we could not get the hid attributes,
// then what we opened was not a hid device,
// we need to close the handle nonetheless.
Kernel32.CloseHandle(handle);
}
throw new Exception();
}