public static extern IntPtr SetupDiOpenDevRegKey(IntPtr hDeviceInfoSet, ref USBClass.Win32Wrapper.SP_DEVINFO_DATA DeviceInfoData, uint Scope, uint HwProfile, uint KeyType, uint samDesired);
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref USBClass.Win32Wrapper.SP_DEVINFO_DATA DeviceInfoData, uint Property, ref uint PropertyRegDataType, IntPtr PropertyBuffer, uint PropertyBufferSize, ref uint RequiredSize);
public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, uint MemberIndex, ref USBClass.Win32Wrapper.SP_DEVINFO_DATA DeviceInfoData);
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, ref USBClass.Win32Wrapper.SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref USBClass.Win32Wrapper.SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, uint deviceInterfaceDetailDataSize, out uint requiredSize, ref USBClass.Win32Wrapper.SP_DEVINFO_DATA deviceInfoData);
public static bool GetUSBDevice(uint VID, uint PID, List <USBClass.DeviceProperties> DPList, bool GetCOMPort, uint?MI = null) { IntPtr num1 = Marshal.AllocHGlobal(1024); IntPtr num2 = IntPtr.Zero; DPList.Clear(); try { var Enumerator = "USB"; var empty = string.Empty; var str = string.Empty; var lowerInvariant1 = ("VID_" + VID.ToString("X4") + "&PID_" + PID.ToString("X4")).ToLowerInvariant(); if (MI.HasValue) { str = ("MI_" + MI.Value.ToString("X2")).ToLowerInvariant(); } num2 = Win32Wrapper.SetupDiGetClassDevs(IntPtr.Zero, Enumerator, IntPtr.Zero, 6); if (num2.ToInt64() != -1L) { var flag = true; uint MemberIndex = 0; while (flag) { if (flag) { uint RequiredSize = 0; uint PropertyRegDataType = 0; var DeviceInfoData = new USBClass.Win32Wrapper.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = (uint)Marshal.SizeOf <USBClass.Win32Wrapper.SP_DEVINFO_DATA>(DeviceInfoData); flag = Win32Wrapper.SetupDiEnumDeviceInfo(num2, MemberIndex, ref DeviceInfoData); if (flag) { Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 1U, ref PropertyRegDataType, IntPtr.Zero, 0U, ref RequiredSize); if (Marshal.GetLastWin32Error() == 122 && RequiredSize <= 1024U && Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 1U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { var lowerInvariant2 = Marshal.PtrToStringAuto(num1).ToLowerInvariant(); if (lowerInvariant2.Contains(lowerInvariant1) && (MI.HasValue && lowerInvariant2.Contains(str) || !MI.HasValue && !lowerInvariant2.Contains("mi"))) { var deviceProperties = new DeviceProperties { FriendlyName = string.Empty }; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 12U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.FriendlyName = Marshal.PtrToStringAuto(num1); } deviceProperties.DeviceType = string.Empty; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 25U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.DeviceType = Marshal.PtrToStringAuto(num1); } deviceProperties.DeviceClass = string.Empty; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 7U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.DeviceClass = Marshal.PtrToStringAuto(num1); } deviceProperties.DeviceManufacturer = string.Empty; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 11U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.DeviceManufacturer = Marshal.PtrToStringAuto(num1); } deviceProperties.DeviceLocation = string.Empty; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 13U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.DeviceLocation = Marshal.PtrToStringAuto(num1); } deviceProperties.DevicePath = string.Empty; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 35U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.DevicePath = Marshal.PtrToStringAuto(num1); } deviceProperties.DevicePhysicalObjectName = string.Empty; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 14U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.DevicePhysicalObjectName = Marshal.PtrToStringAuto(num1); } deviceProperties.DeviceDescription = string.Empty; if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(num2, ref DeviceInfoData, 0U, ref PropertyRegDataType, num1, 1024U, ref RequiredSize)) { deviceProperties.DeviceDescription = Marshal.PtrToStringAuto(num1); } deviceProperties.COMPort = string.Empty; if (GetCOMPort) { IntPtr hKey = Win32Wrapper.SetupDiOpenDevRegKey(num2, ref DeviceInfoData, 1U, 0U, 1U, 131097U); if (hKey.ToInt32() == -1) { deviceProperties.COMPort = null; } else { var lpData = new StringBuilder(1024); var capacity = (uint)lpData.Capacity; if (Win32Wrapper.RegQueryValueEx(hKey, "PortName", 0U, out var lpType, lpData, ref capacity) == 0) { deviceProperties.COMPort = lpData.ToString(); } Win32Wrapper.RegCloseKey(hKey); } } DPList.Add(deviceProperties); } } } } else { var lastWin32Error = (long)Marshal.GetLastWin32Error(); } ++MemberIndex; } } return(DPList.Count > 0); } catch (Exception ex) { throw ex; } finally { Win32Wrapper.SetupDiDestroyDeviceInfoList(num2); Marshal.FreeHGlobal(num1); } }