Java中COM端口上的友好名称?

Java中COM端口上的友好名称?,java,Java,我在一个项目中工作,我必须在COM端口访问。我目前正在使用winjcom访问并列出连接的设备。我的问题是,我希望显示友好的名称,而不仅仅是COM3、COM4等等 我找到一根线 它可以得到友好的名称,但这是基于我知道PID、VID和设备ID的脸。我想在所有连接的设备上都得到它 希望你们能帮助我。:) 作为示例,我使用 调用WinRegistry.getStringSubKeys()。 请参阅,您不需要知道设备id。 您可以使用相同的逻辑,但可能必须在代码中使用递归来遍历较低的分支。作为示例,我使

我在一个项目中工作,我必须在COM端口访问。我目前正在使用winjcom访问并列出连接的设备。我的问题是,我希望显示友好的名称,而不仅仅是COM3、COM4等等

我找到一根线

它可以得到友好的名称,但这是基于我知道PID、VID和设备ID的脸。我想在所有连接的设备上都得到它


希望你们能帮助我。:)

作为示例,我使用 调用WinRegistry.getStringSubKeys()。 请参阅,您不需要知道设备id。
您可以使用相同的逻辑,但可能必须在代码中使用递归来遍历较低的分支。

作为示例,我使用 调用WinRegistry.getStringSubKeys()。 请参阅,您不需要知道设备id。 您可以使用相同的逻辑,但可能必须在代码中使用递归来遍历较低的分支。

使用JNA()并查看下一页。这可能是一个有用的起点

使用JNA()并查看下一页。这可能是一个有用的起点


此代码通过JNA使用windows dll setupapi获取COM端口的描述:

final SetupApi2 setupApi = SetupApi2.INSTANCE;
int SPDRP_FRIENDLYNAME = 12;

WinNT.HANDLE handle = setupApi.SetupDiGetClassDevsA(null, null, null, SetupApi.DIGCF_ALLCLASSES);

SetupApi.SP_DEVINFO_DATA devInfo = new SetupApi.SP_DEVINFO_DATA();
int indice = 0;

while (setupApi.SetupDiEnumDeviceInfo (handle, indice, devInfo) 
    ) {
        char[] windowText1 = new char[255];
    IntByReference flag = new IntByReference(0);

    IntByReference PropertyRegDataType = new IntByReference();
    byte[] descripcionByte = new byte[255];

    if ((setupApi.SetupDiGetDeviceInstanceIdA(handle, devInfo, windowText1, windowText1.length, flag))) {
        if ((setupApi.SetupDiGetDeviceRegistryPropertyA(handle, devInfo, SPDRP_FRIENDLYNAME, PropertyRegDataType, descripcionByte, descripcionByte.length, flag))) {
            String descripcionString = Native.toString(descripcionByte);
            System.out.println(descripcionString);
        }
    }
    indice++;
}

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.Guid.GUID;
import com.sun.jna.platform.win32.SetupApi;
import com.sun.jna.platform.win32.SetupApi.SP_DEVICE_INTERFACE_DATA;
import com.sun.jna.platform.win32.SetupApi.SP_DEVINFO_DATA;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
import java.util.Arrays;
import java.util.List;

public interface SetupApi2 extends Library {

SetupApi2 INSTANCE = (SetupApi2) Native.loadLibrary("setupapi", SetupApi2.class);

public static class HDEVINFO extends WinNT.HANDLE {
}

public static class SP_DRVINFO_DATA extends Structure {

    public SP_DRVINFO_DATA() {
        DriverDate = new WinBase.FILETIME();
    }
    public int cbSize;
    public int DriverType;
    public int Reserved;
    public char[] Description = new char[255];
    public char[] MfgName = new char[255];
    public char[] ProviderName = new char[255];
    public WinBase.FILETIME DriverDate;
    public int DriverVersion;

    protected List getFieldOrder() {
        return Arrays.asList("cbSize",
                "DriverType",
                "Reserved",
                "Description",
                "MfgName",
                "ProviderName",
                "DriverDate",
                "DriverVersion");
    }
}

public static class SP_DEVICE_INTERFACE_DETAIL_DATA extends Structure {

    public static class ByReference extends SP_DEVINFO_DATA implements Structure.ByReference {

        public ByReference() {
        }

        public ByReference(Pointer memory) {
            super(memory);
        }
    }

    public SP_DEVICE_INTERFACE_DETAIL_DATA() {
        cbSize = size();
        setAlignType(Structure.ALIGN_NONE);
    }

    public SP_DEVICE_INTERFACE_DETAIL_DATA(int size) {
        cbSize = size();
        devicePath = new char[size];
        setAlignType(Structure.ALIGN_NONE);
    }

    public SP_DEVICE_INTERFACE_DETAIL_DATA(Pointer memory) {
        super(memory);
        read();
    }

    /**
     * The size, in bytes, of the SP_DEVINFO_DATA structure.
     */
    public int cbSize;

    public char[] devicePath = new char[1];

    protected List getFieldOrder() {
        return Arrays.asList("cbSize",
                "devicePath");
    }
}

public static int DIGCF_DEFAULT = 0x00000001;
public static int DIGCF_PRESENT = 0x00000002;
public static int DIGCF_ALLCLASSES = 0x00000004;
public static int DIGCF_PROFILE = 0x00000008;
public static int DIGCF_DEVICEINTERFACE = 0x00000010;
public static int SPDRP_DRIVER = 0x00000009;
public static int SPDRP_INSTALL_STATE = 0x00000022;

WinNT.HANDLE SetupDiGetClassDevsA(GUID Guid, String Enumerator, WinDef.HWND Parent, int Flags);

boolean SetupDiEnumDeviceInfo(WinNT.HANDLE DeviceInfoSet, int MemberIndex, SP_DEVINFO_DATA DeviceInfoData);

int SetupDiEnumDeviceInterfaces(HDEVINFO DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, GUID Guid, int MemberIndex, SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);

int SetupDiGetDeviceInterfaceDetail(HDEVINFO DeviceInfoSet, SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, IntByReference RequiredSize, SP_DEVINFO_DATA DeviceInfoData);

int SetupDiDestroyDeviceInfoList(HDEVINFO DeviceInfoSet);

boolean SetupDiGetDeviceInstanceIdA(WinNT.HANDLE DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, char[] DeviceId, int DeviceInstanceIdSize, IntByReference RequiredSize);

boolean SetupDiClassNameFromGuid(GUID Guid, char[] ClassName, int ClassNameSize, IntByReference RequiredSize);

boolean SetupDiGetSelectedDriver(HDEVINFO DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SP_DRVINFO_DATA DriverInfoData);

boolean SetupDiClassGuidsFromName(String ClassName, GUID[] ClassGuidList, int ClassGuidListSize, IntByReference RequiredSize);

boolean SetupDiGetDeviceRegistryPropertyA(WinNT.HANDLE DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, int Property, IntByReference PropertyRegDataType, byte[] PropertyBuffer, int PropertyBufferSize, IntByReference RequiredSize);
}

此代码通过JNA使用windows dll setupapi获取COM端口的说明:

final SetupApi2 setupApi = SetupApi2.INSTANCE;
int SPDRP_FRIENDLYNAME = 12;

WinNT.HANDLE handle = setupApi.SetupDiGetClassDevsA(null, null, null, SetupApi.DIGCF_ALLCLASSES);

SetupApi.SP_DEVINFO_DATA devInfo = new SetupApi.SP_DEVINFO_DATA();
int indice = 0;

while (setupApi.SetupDiEnumDeviceInfo (handle, indice, devInfo) 
    ) {
        char[] windowText1 = new char[255];
    IntByReference flag = new IntByReference(0);

    IntByReference PropertyRegDataType = new IntByReference();
    byte[] descripcionByte = new byte[255];

    if ((setupApi.SetupDiGetDeviceInstanceIdA(handle, devInfo, windowText1, windowText1.length, flag))) {
        if ((setupApi.SetupDiGetDeviceRegistryPropertyA(handle, devInfo, SPDRP_FRIENDLYNAME, PropertyRegDataType, descripcionByte, descripcionByte.length, flag))) {
            String descripcionString = Native.toString(descripcionByte);
            System.out.println(descripcionString);
        }
    }
    indice++;
}

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.Guid.GUID;
import com.sun.jna.platform.win32.SetupApi;
import com.sun.jna.platform.win32.SetupApi.SP_DEVICE_INTERFACE_DATA;
import com.sun.jna.platform.win32.SetupApi.SP_DEVINFO_DATA;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
import java.util.Arrays;
import java.util.List;

public interface SetupApi2 extends Library {

SetupApi2 INSTANCE = (SetupApi2) Native.loadLibrary("setupapi", SetupApi2.class);

public static class HDEVINFO extends WinNT.HANDLE {
}

public static class SP_DRVINFO_DATA extends Structure {

    public SP_DRVINFO_DATA() {
        DriverDate = new WinBase.FILETIME();
    }
    public int cbSize;
    public int DriverType;
    public int Reserved;
    public char[] Description = new char[255];
    public char[] MfgName = new char[255];
    public char[] ProviderName = new char[255];
    public WinBase.FILETIME DriverDate;
    public int DriverVersion;

    protected List getFieldOrder() {
        return Arrays.asList("cbSize",
                "DriverType",
                "Reserved",
                "Description",
                "MfgName",
                "ProviderName",
                "DriverDate",
                "DriverVersion");
    }
}

public static class SP_DEVICE_INTERFACE_DETAIL_DATA extends Structure {

    public static class ByReference extends SP_DEVINFO_DATA implements Structure.ByReference {

        public ByReference() {
        }

        public ByReference(Pointer memory) {
            super(memory);
        }
    }

    public SP_DEVICE_INTERFACE_DETAIL_DATA() {
        cbSize = size();
        setAlignType(Structure.ALIGN_NONE);
    }

    public SP_DEVICE_INTERFACE_DETAIL_DATA(int size) {
        cbSize = size();
        devicePath = new char[size];
        setAlignType(Structure.ALIGN_NONE);
    }

    public SP_DEVICE_INTERFACE_DETAIL_DATA(Pointer memory) {
        super(memory);
        read();
    }

    /**
     * The size, in bytes, of the SP_DEVINFO_DATA structure.
     */
    public int cbSize;

    public char[] devicePath = new char[1];

    protected List getFieldOrder() {
        return Arrays.asList("cbSize",
                "devicePath");
    }
}

public static int DIGCF_DEFAULT = 0x00000001;
public static int DIGCF_PRESENT = 0x00000002;
public static int DIGCF_ALLCLASSES = 0x00000004;
public static int DIGCF_PROFILE = 0x00000008;
public static int DIGCF_DEVICEINTERFACE = 0x00000010;
public static int SPDRP_DRIVER = 0x00000009;
public static int SPDRP_INSTALL_STATE = 0x00000022;

WinNT.HANDLE SetupDiGetClassDevsA(GUID Guid, String Enumerator, WinDef.HWND Parent, int Flags);

boolean SetupDiEnumDeviceInfo(WinNT.HANDLE DeviceInfoSet, int MemberIndex, SP_DEVINFO_DATA DeviceInfoData);

int SetupDiEnumDeviceInterfaces(HDEVINFO DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, GUID Guid, int MemberIndex, SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);

int SetupDiGetDeviceInterfaceDetail(HDEVINFO DeviceInfoSet, SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, IntByReference RequiredSize, SP_DEVINFO_DATA DeviceInfoData);

int SetupDiDestroyDeviceInfoList(HDEVINFO DeviceInfoSet);

boolean SetupDiGetDeviceInstanceIdA(WinNT.HANDLE DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, char[] DeviceId, int DeviceInstanceIdSize, IntByReference RequiredSize);

boolean SetupDiClassNameFromGuid(GUID Guid, char[] ClassName, int ClassNameSize, IntByReference RequiredSize);

boolean SetupDiGetSelectedDriver(HDEVINFO DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SP_DRVINFO_DATA DriverInfoData);

boolean SetupDiClassGuidsFromName(String ClassName, GUID[] ClassGuidList, int ClassGuidListSize, IntByReference RequiredSize);

boolean SetupDiGetDeviceRegistryPropertyA(WinNT.HANDLE DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, int Property, IntByReference PropertyRegDataType, byte[] PropertyBuffer, int PropertyBufferSize, IntByReference RequiredSize);
}