private static List <HidDevice.DeviceData> PopulateDeviceList(int vendorId, int productId) { SafeFileHandle safeFileHandle = (SafeFileHandle)null; List <HidDevice.DeviceData> list = new List <HidDevice.DeviceData>(); Guid gHid; SemtechLib.Devices.Common.NativeMethods.HidD_GetHidGuid(out gHid); IntPtr classDevs = SemtechLib.Devices.Common.NativeMethods.SetupDiGetClassDevs(ref gHid, IntPtr.Zero, IntPtr.Zero, 18); string vid_pid = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "vid_{0:x4}&pid_{1:x4}", vendorId, productId ); try { SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); int deviceIndex = 0; while (SemtechLib.Devices.Common.NativeMethods.SetupDiEnumDeviceInterfaces(classDevs, IntPtr.Zero, ref gHid, deviceIndex++, ref deviceInterfaceData)) { // Scan device path for VIS/PID string devicePath = HidDevice.GetDevicePath(classDevs, ref deviceInterfaceData); if (devicePath.IndexOf(vid_pid) >= 0) { if (safeFileHandle != null) { safeFileHandle.Close(); } safeFileHandle = SemtechLib.Devices.Common.NativeMethods.CreateFile(devicePath, -1073741824, 0, IntPtr.Zero, 3, 0, IntPtr.Zero); if (Marshal.GetLastWin32Error() == 0) { StringBuilder lpBuffer = new StringBuilder(); if (SemtechLib.Devices.Common.NativeMethods.HidD_GetManufacturerString(safeFileHandle, lpBuffer, 255)) { StringBuilder lpbuffer = new StringBuilder(); if (SemtechLib.Devices.Common.NativeMethods.HidD_GetProductString(safeFileHandle, lpbuffer, 255)) { list.Add(new HidDevice.DeviceData(devicePath, lpBuffer.ToString(), lpbuffer.ToString())); } } } } } return(list); } finally { if (safeFileHandle != null) { safeFileHandle.Close(); } SemtechLib.Devices.Common.NativeMethods.SetupDiDestroyDeviceInfoList(classDevs); } }
private static string GetDevicePath(IntPtr DeviceInfoTable, ref SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DATA InterfaceDataStructure) { int RequiredSize = 0; SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData = new SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA(); interfaceDetailData.cbSize = Marshal.SizeOf(interfaceDetailData); SemtechLib.Devices.Common.NativeMethods.SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref InterfaceDataStructure, IntPtr.Zero, 0, ref RequiredSize, IntPtr.Zero); IntPtr hDevice = Marshal.AllocHGlobal(RequiredSize); interfaceDetailData.cbSize = IntPtr.Size != 8 ? 4 + Marshal.SystemDefaultCharSize : 8; Marshal.StructureToPtr(interfaceDetailData, hDevice, false); if (SemtechLib.Devices.Common.NativeMethods.SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref InterfaceDataStructure, hDevice, RequiredSize, IntPtr.Zero, IntPtr.Zero)) { string devicePath = Marshal.PtrToStringUni(new IntPtr(hDevice.ToInt32() + 4)); Marshal.FreeHGlobal(hDevice); return(devicePath); } Marshal.FreeHGlobal(hDevice); return(string.Empty); }
private static List<HidDevice.DeviceData> PopulateDeviceList(int vendorId, int productId) { SafeFileHandle safeFileHandle = (SafeFileHandle)null; List<HidDevice.DeviceData> list = new List<HidDevice.DeviceData>(); Guid gHid; SemtechLib.Devices.Common.NativeMethods.HidD_GetHidGuid(out gHid); IntPtr classDevs = SemtechLib.Devices.Common.NativeMethods.SetupDiGetClassDevs(ref gHid, IntPtr.Zero, IntPtr.Zero, 18); string vid_pid = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "vid_{0:x4}&pid_{1:x4}", vendorId, productId ); try { SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); int deviceIndex = 0; while (SemtechLib.Devices.Common.NativeMethods.SetupDiEnumDeviceInterfaces(classDevs, IntPtr.Zero, ref gHid, deviceIndex++, ref deviceInterfaceData)) { // Scan device path for VIS/PID string devicePath = HidDevice.GetDevicePath(classDevs, ref deviceInterfaceData); if (devicePath.IndexOf(vid_pid) >= 0) { if (safeFileHandle != null) safeFileHandle.Close(); safeFileHandle = SemtechLib.Devices.Common.NativeMethods.CreateFile(devicePath, -1073741824, 0, IntPtr.Zero, 3, 0, IntPtr.Zero); if (Marshal.GetLastWin32Error() == 0) { StringBuilder lpBuffer = new StringBuilder(); if (SemtechLib.Devices.Common.NativeMethods.HidD_GetManufacturerString(safeFileHandle, lpBuffer, 255)) { StringBuilder lpbuffer = new StringBuilder(); if (SemtechLib.Devices.Common.NativeMethods.HidD_GetProductString(safeFileHandle, lpbuffer, 255)) list.Add(new HidDevice.DeviceData(devicePath, lpBuffer.ToString(), lpbuffer.ToString())); } } } } return list; } finally { if (safeFileHandle != null) safeFileHandle.Close(); SemtechLib.Devices.Common.NativeMethods.SetupDiDestroyDeviceInfoList(classDevs); } }