public static IEnumerable<HidDevice> GetDevices() { Guid hidGuid = HidDevice.HIDGuid; // Get the list of HID devices IntPtr deviceInfoSet = new IntPtr(); deviceInfoSet = setup.SetupDiGetClassDevs(ref hidGuid, IntPtr.Zero, IntPtr.Zero, setup.DIGCF_PRESENT | setup.DIGCF_DEVICEINTERFACE); int memberIndex = 0; SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData); bool success; bool lastDevice = false; int bufferSize = 0; UnmanagedMemory detailDataBuffer; List<HidDevice> devicePathNames = new List<HidDevice>(); bool deviceFound = false; do { success = setup.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref hidGuid, memberIndex, ref MyDeviceInterfaceData); if (!success) { lastDevice = true; } else { // First call to get size of data buffer success = setup.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref MyDeviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero); // Allocate memory for the SP_DEVICE_INTERFACE_DETAIL_DATA structure using the returned buffer size detailDataBuffer = new UnmanagedMemory(bufferSize); // Store cbSize in the first bytes of the array. The number of bytes varies with 32- and 64-bit systems. Marshal.WriteInt32(detailDataBuffer.MemoryPointer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8); // Call SetupDiGetDeviceInterfaceDetail again. // This time, pass a pointer to DetailDataBuffer // and the returned required buffer size. success = setup.SetupDiGetDeviceInterfaceDetail (deviceInfoSet, ref MyDeviceInterfaceData, detailDataBuffer.MemoryPointer, bufferSize, ref bufferSize, IntPtr.Zero); //diDetail.cbSize = (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8; //SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA(); //diDetail.cbSize = Marshal.SizeOf(diDetail); //success = setup.SetupDiGetDeviceInterfaceDetail // (deviceInfoSet, // ref MyDeviceInterfaceData, // ref diDetail, // diDetail.cbSize, // ref bufferSize, // IntPtr.Zero); IntPtr pDevicePathName = detailDataBuffer.MemoryPointer + 4; string devicePathName = Marshal.PtrToStringAuto(pDevicePathName); HidDevice newDevice = new HidDevice(devicePathName); devicePathNames.Add(newDevice); deviceFound = true; } memberIndex++; } while (!lastDevice); // Destroy the device Info Set to cleanup memory setup.SetupDiDestroyDeviceInfoList(deviceInfoSet); return devicePathNames; }
/// <summary> /// Construction. Setup the buffer with the correct output report length dictated by the device /// </summary> /// <param name="oDev">Creating device</param> public OutputReport(HidDevice oDev) : base(oDev) { SetBuffer(new byte[oDev.outputReportLength]); }
/// <summary> /// Construction. Do nothing /// </summary> /// <param name="oDev">Creating device</param> public InputReport(HidDevice oDev) : base(oDev) { }
/// <summary> /// Constructor /// </summary> /// <param name="oDev">Constructing device</param> public Report(HidDevice oDev) { // Do nothing }