public static extern Int32 HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
void Initialize(string devicePathName) { DevicePath = devicePathName; // Get the vendor and product ids out of the path int startIndex = 0; startIndex = devicePathName.IndexOf("vid_", 0); string vendorIDstring = "0x" + devicePathName.Substring(startIndex + 4, 4); VendorID = Convert.ToInt16(vendorIDstring, 16); startIndex = devicePathName.IndexOf("pid_", 0); string productID = "0x" + devicePathName.Substring(startIndex + 4, 4); ProductID = Convert.ToInt16(productID, 16); // Try to open up the device DeviceFileHandle = Kernel32.CreateFile(DevicePath, (uint)(Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE), (uint)0, (IntPtr)IntPtr.Zero, (uint)Kernel32.OPEN_EXISTING, (uint)Kernel32.FILE_FLAG_OVERLAPPED, (IntPtr)IntPtr.Zero); //m_oFile = new FileStream(DevicePath, // FileMode.Open, FileAccess.ReadWrite); // If we had a problem trying to open the device file handle // throw an exception if (DeviceFileHandle.IsInvalid) { DeviceFileHandle = null; return; //throw HIDDeviceException.GenerateWithWinError("Failed to create device file"); } // Try to get the device capabilities IntPtr lpData = new IntPtr(0) ; if (Hid.HidD_GetPreparsedData(DeviceFileHandle, ref lpData)) // get windows to read the device data into an internal buffer { try { HIDP_CAPS oCaps = new HIDP_CAPS(); Hid.HidP_GetCaps(lpData, ref oCaps); // extract the device capabilities from the internal buffer inputReportLength = oCaps.InputReportByteLength; // get the input... outputReportLength = oCaps.OutputReportByteLength; // ... and output report lengths //m_oFile = new FileStream(m_hHandle, FileAccess.Read | FileAccess.Write, true, m_nInputReportLength, true); m_oFile = new FileStream(DeviceFileHandle, FileAccess.Read | FileAccess.Write, inputReportLength, true); BeginAsyncRead(); // kick off the first asynchronous read } catch (Exception ex) { throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid."); } finally { Hid.HidD_FreePreparsedData(lpData); // before we quit the funtion, we must free the internal buffer reserved in GetPreparsedData } } else // GetPreparsedData failed? Chuck an exception { throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed"); } }