/// <summary> /// Class constructor will fetch this object properties from HID sub system. /// </summary> /// <param name="hRawInputDevice"> /// Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as /// rawinput.header.hDevice /// </param> public HidDevice(IntPtr hRawInputDevice) { //Fetch various information defining the given HID device Name = RawInput.GetDeviceName(hRawInputDevice); //Open our device from the device name/path var handle = Function.CreateFile(Name, FileAccess.NONE, FileShare.FILE_SHARE_READ | FileShare.FILE_SHARE_WRITE, IntPtr.Zero, CreationDisposition.OPEN_EXISTING, FileFlagsAttributes.FILE_FLAG_OVERLAPPED, IntPtr.Zero ); if (handle.IsInvalid) { Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error()); } else { //Get manufacturer string var manufacturerString = new StringBuilder(256); if (Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity)) { Manufacturer = manufacturerString.ToString(); } //Get product string var productString = new StringBuilder(256); if (Function.HidD_GetProductString(handle, productString, productString.Capacity)) { Product = productString.ToString(); } //Get attributes var attributes = new HIDD_ATTRIBUTES(); if (Function.HidD_GetAttributes(handle, ref attributes)) { VendorId = attributes.VendorID; ProductId = attributes.ProductID; Version = attributes.VersionNumber; } handle.Close(); } }
public static extern Boolean HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);