JAVA代码获取U盘的唯一Id或唯一标识
时间: 2023-12-10 20:37:47 浏览: 52
在Java中,可以使用JNA库来获取U盘的唯一ID或唯一标识。以下是一个示例代码:
```java
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
public class USBUtil {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class);
int ioctl(int fd, long cmd, Pointer arg);
}
public static String getSerialNumber(String drive) {
try {
String serial = null;
if (drive.contains(":"))
drive = drive.substring(0, 2);
File file = new File(drive);
if (!file.exists())
return null;
String diskLetter = file.getAbsolutePath();
if (!diskLetter.endsWith("\\"))
diskLetter += "\\";
Kernel32.SYSTEM_DEVICE_INFORMATION.ByReference byRef = new Kernel32.SYSTEM_DEVICE_INFORMATION.ByReference();
Memory memory = new Memory(byRef.size());
memory.clear();
byRef.memory = memory;
byRef.size = memory.size();
long result = Kernel32.INSTANCE.ZwQuerySystemInformation(SystemDeviceInformation, byRef.getPointer(), byRef.size, null);
if (result != 0)
throw new RuntimeException("ZwQuerySystemInformation failed. Error: " + result);
Kernel32.SYSTEM_DEVICE_INFORMATION deviceInfo = new Kernel32.SYSTEM_DEVICE_INFORMATION(byRef.memory);
int diskNumber = -1;
for (int i = 0; i < deviceInfo.NumberOfDisks; i++) {
Kernel32.SYSTEM_DEVICE_INFORMATION.SYSTEM_DEVICE device = deviceInfo.getDisk(i);
if (diskLetter.equals(device.SymbolicLinkName)) {
diskNumber = device.DeviceNumber;
break;
}
}
if (diskNumber == -1)
return null;
String path = "\\\\.\\PhysicalDrive" + diskNumber;
HANDLE hdev = Kernel32.INSTANCE.CreateFile(path,
Kernel32.GENERIC_READ,
Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE,
null,
Kernel32.OPEN_EXISTING,
0,
null);
if (hdev != WinBase.INVALID_HANDLE_VALUE) {
STORAGE_PROPERTY_QUERY query = new STORAGE_PROPERTY_QUERY();
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
int dwOutBufferSize = 1024;
Memory outBuf = new Memory(dwOutBufferSize);
boolean status = Kernel32.INSTANCE.DeviceIoControl(hdev,
IOCTL_STORAGE_QUERY_PROPERTY,
query.getPointer(),
query.size(),
outBuf,
dwOutBufferSize,
new IntByReference(),
null);
if (status) {
STORAGE_DEVICE_DESCRIPTOR descriptor = new STORAGE_DEVICE_DESCRIPTOR(outBuf);
serial = descriptor.getSerialNumber();
}
Kernel32.INSTANCE.CloseHandle(hdev);
}
return serial;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static final int FILE_DEVICE_DISK = 0x00000007;
public static final int IOCTL_STORAGE_QUERY_PROPERTY = 0x2D1400;
public static final int StorageDeviceProperty = 0;
public static final int PropertyStandardQuery = 0;
public static final int SystemDeviceInformation = 11;
public static interface Kernel32 extends com.sun.jna.platform.win32.Kernel32 {
public static Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
public HANDLE CreateFile(String lpFileName, int dwDesiredAccess, int dwShareMode, SECURITY_ATTRIBUTES lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, HANDLE hTemplateFile);
public boolean DeviceIoControl(HANDLE hDevice, int dwIoControlCode, Structure lpInBuffer, int nInBufferSize, Structure lpOutBuffer, int nOutBufferSize, IntByReference lpBytesReturned, Pointer lpOverlapped);
public boolean CloseHandle(HANDLE hObject);
public int ZwQuerySystemInformation(int SystemInformationClass, Pointer SystemInformation, int SystemInformationLength, IntByReference ReturnLength);
public static class SYSTEM_DEVICE_INFORMATION extends Structure {
public int NumberOfDisks;
public static class SYSTEM_DEVICE extends Structure {
public int DeviceType;
public int DeviceNumber;
public int PartitionNumber;
public int AlignmentRequirement;
public String SymbolicLinkName;
public String DeviceName;
}
public SYSTEM_DEVICE_INFORMATION.SYSTEM_DEVICE getDisk(int index) {
return (SYSTEM_DEVICE_INFORMATION.SYSTEM_DEVICE) this.fields[index];
}
public SYSTEM_DEVICE_INFORMATION(Pointer memory) {
super(memory);
int offset = 0;
this.NumberOfDisks = this.getInt(offset);
offset += new NativeLong().size();
for (int i = 0; i < this.NumberOfDisks; i++) {
SYSTEM_DEVICE_INFORMATION.SYSTEM_DEVICE device = new SYSTEM_DEVICE_INFORMATION.SYSTEM_DEVICE();
device.DeviceType = this.getInt(offset);
offset += new NativeLong().size();
device.DeviceNumber = this.getInt(offset);
offset += new NativeLong().size();
device.PartitionNumber = this.getInt(offset);
offset += new NativeLong().size();
device.AlignmentRequirement = this.getInt(offset);
offset += new NativeLong().size();
Pointer ptr = this.getPointer(offset);
device.SymbolicLinkName = ptr.getString(0, true);
offset += new Pointer(0).size();
ptr = this.getPointer(offset);
device.DeviceName = ptr.getString(0, true);
offset += new Pointer(0).size();
this.fields[i] = device;
}
}
public SYSTEM_DEVICE_INFORMATION() {
this(Structure.ALIGN_NONE);
}
public SYSTEM_DEVICE_INFORMATION(int alignment) {
super(alignment);
}
public static class ByReference extends SYSTEM_DEVICE_INFORMATION implements Structure.ByReference {
}
}
public static class STORAGE_PROPERTY_QUERY extends Structure {
public int PropertyId;
public int QueryType;
public STORAGE_PROPERTY_QUERY() {
}
public STORAGE_PROPERTY_QUERY(int propertyId, int queryType) {
this.PropertyId = propertyId;
this.QueryType = queryType;
}
protected List getFieldOrder() {
return Arrays.asList("PropertyId", "QueryType");
}
}
public static class STORAGE_DEVICE_DESCRIPTOR extends Structure {
public int Version;
public int Size;
public byte DeviceType;
public byte DeviceTypeModifier;
public byte RemovableMedia;
public byte CommandQueueing;
public int VendorIdOffset;
public int ProductIdOffset;
public int ProductRevisionOffset;
public int SerialNumberOffset;
public STORAGE_DEVICE_DESCRIPTOR() {
}
public STORAGE_DEVICE_DESCRIPTOR(Pointer memory) {
super(memory);
this.read();
}
public String getSerialNumber() {
byte[] buffer = new byte[127];
this.getPointer().read(this.SerialNumberOffset, buffer, 0, buffer.length);
return new String(buffer).trim();
}
}
}
}
```
使用方法:
```java
String serialNumber = USBUtil.getSerialNumber("D:\\");
System.out.println(serialNumber);
```
其中,参数"D:\\"表示U盘的驱动器号。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)