获取USB 3.0设备的驱动器号(Windows下的Java)

获取USB 3.0设备的驱动器号(Windows下的Java),java,windows,powershell,usb-drive,usb4java,Java,Windows,Powershell,Usb Drive,Usb4java,我的Java程序需要获得连接的USB设备的驱动器号列表,但只需要那些支持USB 3.0的设备(设备及其插入的USB端口,以便高速工作) 目前,我尝试通过我的Java程序执行的PowerShell命令使用WMI 我已经找到了这个:。但它也会列出USB2.0设备 关于版本检测,我发现:-我尝试的PowerShell命令是Get WmiObject Win32\u USBHub。这带来了几个问题。第一:它列出的东西远远不止USB驱动器(我想我的电脑上也有USB集线器)。第二:即使列表中的所有项目都有一

我的Java程序需要获得连接的USB设备的驱动器号列表,但只需要那些支持USB 3.0的设备(设备及其插入的USB端口,以便高速工作)

目前,我尝试通过我的Java程序执行的PowerShell命令使用WMI

我已经找到了这个:。但它也会列出USB2.0设备

关于版本检测,我发现:-我尝试的PowerShell命令是
Get WmiObject Win32\u USBHub
。这带来了几个问题。第一:它列出的东西远远不止USB驱动器(我想我的电脑上也有USB集线器)。第二:即使列表中的所有项目都有一个USBVersion字段,但它始终为空

更新 我过去几天研究的本质是,我需要连接两个领域的信息

  • 驱动器/逻辑驱动器
    • 驱动器号
    • BusType(就我而言等于“USB”)
  • USB设备
    • 供应商ID和产品ID(VID和PID)
    • (usb设备描述符中的值,指示usb版本)
对于给定的驱动器号,我需要找到bcdUSB值。但是我还没有找到一种方法来获取与USB设备对应的驱动器

到目前为止我试过什么 PowerShell上的WMI

我找到的相关命令是

Get-Disk               // Get BusType
gwmi Win32_LogicalDisk // Get drive letter
// Those make the connection between disk and logical disk
gwmi Win32_LogicalDiskToPartition
gwmi Win32_LogicalDiskToPartition
即使我得到了BusType,我也无法连接到bcdUSB

usb4java()

我只从USB设备领域获取信息。我可以加载设备并查看视频和PID以及bcdUSB值,但无法将其映射到驱动器和驱动器号

通过Cygwin的lsusb

根据linux命令,它比WMI更容易处理。所以我试着在Windows下使用它。但是我喜欢usb4java,我只得到了VID&PID+bcdUSB,而不是挂载点(驱动器号)

搜索Windows注册表

我在Windows注册表中进行了一些字符串搜索。没有成功

读取Windows事件日志


我考虑过使用Windows事件来同时检测哪个驱动器和哪个USB设备连接。我甚至在插入U盘时都没有发现事件。

也许这就是您要寻找的:
至少有人将答案标记为有效…:-)

由于建议的链接解决了C#而非Java的这个问题,并且省略了一个步骤,因此我将在这里发布我的最终代码

总结 在爪哇

  • 用于查找所有连接了
    bcdUSB=0x0300的USB设备
  • 获取该设备的供应商ID和产品ID(VID&PID)
通过Powershell(带)

  • 获取给定视频和PID的PnPEntity
  • 获取相关的USB控制器
  • 查找与磁盘驱动器关联的USB控制器的关联器
  • 去拿那个磁盘驱动器
  • 获取相关磁盘分区
  • 获取相关逻辑磁盘->LogicalDisk.DeviceID=驱动器号
代码 Java类:

class UsbDetector {

  private PowerShell shell;

  @PostConstruct
  private void init() {
    shell = com.profesorfalken.jpowershell.PowerShell.openSession();
  }

  @OnDestroy
  private void onShutdownHook() {
    shell.close();
  }

  /**
   * Get drive letters of USB 3.0 devices.
   */
  public List<String> getDriveLettersForUsb3Devices() throws IOException, UsbException {
    List<UsbDevice> devicesUSB3 = getAllUsb3Devices();

    ImmutableList.Builder<String> driveLetterList = ImmutableList.builder();
    for (UsbDevice device : devicesUSB3) {
      String vidAndPid = getVidAndPid(device);

      String powerShellScript = buildScript(vidAndPid);

      String driveLetter = executeOnPowerShell(powerShellScript);
      driveLetterList.add(driveLetter);
    }

    return driveLetterList.build();
  }

  private String executeOnPowerShell(String powerShellScript) {
    InputStream psScriptStream = new ByteArrayInputStream(powerShellScript.getBytes());
    BufferedReader psScriptReader = new BufferedReader(new InputStreamReader(psScriptStream));

    PowerShellResponse response = shell.executeScript(psScriptReader);

    return response.getCommandOutput();
  }

  private String buildScript(String vidAndPid) throws IOException {
    InputStream psScriptStream =
        getClass().getClassLoader().getResourceAsStream("GetUsbDrives.ps1");

    String psScript = IOUtil.toString(psScriptStream);

    psScript = String.format("$input=\"%s\"", vidAndPid) + "\n" + psScript;
    return psScript;
  }

  /**
   * The Vendor ID and Product ID are necessary to find the device via WMI.
   */
  private String getVidAndPid(UsbDevice device) {
    short vendorId = device.getUsbDeviceDescriptor().idVendor();
    short productId = device.getUsbDeviceDescriptor().idProduct();

    String vendorIdHexString = String.format("%04x", vendorId).toUpperCase();
    String productIdHexString = String.format("%04x", productId).toUpperCase();

    String vidAndPid = String.format("VID_%s&PID_%s", vendorIdHexString, productIdHexString);
    return vidAndPid;
  }

  /**
   * From all Usb devices find those with USB 3.0. The value bcdUsb is a hexadecimal coded number
   * telling us the USB version.
   */
  private List<UsbDevice> getAllUsb3Devices() throws UsbException {
    List<UsbDevice> devicesUSB3 = Lists.newArrayList();
    UsbServices services = new org.usb4java.javax.Services();

    UsbHub hub = services.getRootUsbHub();
    List<UsbDevice> devices = getAllUsbDevices(hub);
    for (UsbDevice device : devices) {
      UsbDeviceDescriptor descriptor = device.getUsbDeviceDescriptor();
      short bcdUsb = descriptor.bcdUSB();
      String bcdDecoded = DescriptorUtils.decodeBCD(bcdUsb);

      if (Objects.equal(bcdDecoded, "3.00")) {
        devicesUSB3.add(device);
      }
    }
    return devicesUSB3;
  }

  /**
   * UsbHubs can either mount UsbDevices or further UsbHubs. This method searches through the tree
   * of UsbHubs for UsbDevices and returns them as list.
   */
  private List<UsbDevice> getAllUsbDevices(UsbHub hub) {
    List<UsbDevice> devices = Lists.newArrayList();

    List<UsbDevice> attachedDevices = hub.getAttachedUsbDevices();
    for (UsbDevice device : attachedDevices) {
      if (device instanceof UsbHub) {
        List<UsbDevice> subdevices = getAllUsbDevices((UsbHub) device);
        devices.addAll(subdevices);
      } else {
        devices.add(device);
      }
    }
    return devices;
  }

}
Maven依赖项:

    <dependency>
        <groupId>org.usb4java</groupId>
        <artifactId>usb4java-javax</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.profesorfalken</groupId>
        <artifactId>jPowerShell</artifactId>
        <version>3.1.1</version>
    </dependency>

org.usb4java
usb4javajavax
1.3.0
索法肯教授
鲍威尔
3.1.1

所有这些都取决于程序的用例。你可以用JNI来做。如果有帮助的话,可以提供一些LIB,比如查看。谢谢你的建议。我稍微检查了一下usb4java。有一种方法可以获取连接设备的usb版本,但显然不是相应的驱动器号。到目前为止,它还没有做到这一点。这里是一个开始:它有一些关于UsbDeviceDescriptor对象属性的信息。0x0300的值=USB 3.0.Hi Rich Moss,此超级用户线程是关于显示USB速度的工具。我需要我的Java程序本身来获取这些信息。正如Petermm和Dropout所建议的那样,我甚至用usb4java从Java获取了bcdUSB值,但从那里我没有得到Windows安装设备的驱动器号。
    <dependency>
        <groupId>org.usb4java</groupId>
        <artifactId>usb4java-javax</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.profesorfalken</groupId>
        <artifactId>jPowerShell</artifactId>
        <version>3.1.1</version>
    </dependency>