This worked for me, sorry the code is not very clean, but it works :-)
SAMPLE USAGE:
VolumeInformation info = VolumeInformation.createVolumeInformation("c:\\");
System.out.println(info.getDiskLabel());
System.out.println(info.getDiskSerial());
System.out.println(info.getFileSystem());
FILE VolumeInformation.java
import java.io.File;
public abstract class VolumeInformation {
public static VolumeInformation createVolumeInformation(
String path) {
File pathFile = new File(path);
return createVolumeInformation(pathFile);
}
public static VolumeInformation createVolumeInformation(File pathFile) {
//return new VolumeInformation_win_cmdVol(pathFile);
return new VolumeInformation_win_jni(pathFile);
}
/**
* Returns the serial id of the disk containing
* the specified path
* @return
*/
public abstract String getDiskSerial();
/**
* Returns the label of the disk containing
* the specified path
* @return
*/
public abstract String getDiskLabel();
public abstract String getFileSystem();
}
FILE VolumeInformation_win_jni.java
import java.io.File;
import java.io.IOException;
import java.nio.CharBuffer;
import com.sun.jna.ptr.LongByReference;
class VolumeInformation_win_jni extends VolumeInformation {
private String diskSerial;
private String diskLabel;
private String fileSystem;
public VolumeInformation_win_jni(String path) {
this(new File(path));
}
/**
* @param pathFile
*/
public VolumeInformation_win_jni(File pathFile) {
String driveName;
try {
driveName = pathFile.getCanonicalPath().substring(0, 2) + "\\";
} catch(IOException e) {
throw new RuntimeException(e);
}
//byte[] lpRootPathName = new byte[4];
//for (int i=0; i<3; i++) {
//lpRootPathName[i] = (byte)(((byte)driveName.charAt(i)));
//}
//lpRootPathName[3] = '\0';
//char[] lpRootPathName_chars0 = driveName.toCharArray();
char[] lpRootPathName_chars = new char[4];
for (int i=0; i<3; i++) {
lpRootPathName_chars[i] = driveName.charAt(i);
}
lpRootPathName_chars[3] = '\0';
int nVolumeNameSize = 256;
//String lpRootPathName_string = driveName;
//ByteBuffer lpVolumeNameBuffer_byte = ByteBuffer.allocate(nVolumeNameSize);
CharBuffer lpVolumeNameBuffer_char = CharBuffer.allocate(nVolumeNameSize);
LongByReference lpVolumeSerialNumber = new LongByReference();
LongByReference lpMaximumComponentLength = new LongByReference();
LongByReference lpFileSystemFlags = new LongByReference();
int nFileSystemNameSize = 256;
//ByteBuffer lpFileSystemNameBuffer_byte = ByteBuffer.allocate(nFileSystemNameSize);
CharBuffer lpFileSystemNameBuffer_char = CharBuffer.allocate(nFileSystemNameSize);
//boolean result = MyKernel32.INSTANCE.GetVolumeInformationA(
//lpRootPathName_string,
//lpVolumeNameBuffer_byte,
//nVolumeNameSize,
//lpVolumeSerialNumber,
//lpMaximumComponentLength,
//lpFileSystemFlags,
//lpFileSystemNameBuffer_byte,
//nFileSystemNameSize);
//int err = MyKernel32.INSTANCE.GetLastError();
//this.diskLabel = lpVolumeNameBuffer_byte.toString();
//this.diskSerial = Long.toHexString(lpVolumeSerialNumber.getValue());
//this.fileSystem = lpFileSystemNameBuffer_byte.toString();
boolean result2 = MyKernel32.INSTANCE.GetVolumeInformationW(
lpRootPathName_chars,
lpVolumeNameBuffer_char,
nVolumeNameSize,
lpVolumeSerialNumber,
lpMaximumComponentLength,
lpFileSystemFlags,
lpFileSystemNameBuffer_char,
nFileSystemNameSize);
if (!result2) {
int err2 = MyKernel32.INSTANCE.GetLastError();
throw new RuntimeException("W32Api call to GetVolumeInformationW returned err code " + err2);
}
this.diskLabel = charString2String(lpVolumeNameBuffer_char);
this.fileSystem = charString2String(lpFileSystemNameBuffer_char);
this.diskSerial = Long.toHexString(lpVolumeSerialNumber.getValue());
}
/**
* Returns the serial id of the disk containing
* the specified path
* @return
*/
@Override
public String getDiskSerial() {
return diskSerial;
}
/**
* Returns the label of the disk containing
* the specified path
* @return
*/
@Override
public String getDiskLabel() {
return diskLabel;
}
@Override
public String getFileSystem() {
return fileSystem;
}
private String charString2String(CharBuffer buf) {
char[] chars = buf.array();
int i;
for (i=0; i<chars.length; i++) {
if (chars[i]=='\0') break;
}
return new String(chars,0,i);
}
}