My version \\?\Volume{5b57f944-8d60-11de-8b2a-806d6172696f}\ should get C:\
Kernel32 Interface:
import com.sun.jna.WString; import com.sun.jna.platform.win32.WinDef.DWORD; import com.sun.jna.ptr.IntByReference; import com.sun.jna.win32.StdCallLibrary; public interface Kernel32 extends StdCallLibrary { public boolean GetVolumePathNamesForVolumeNameW( WString lpszVolumeName, char[] lpszVolumePathNames, DWORD cchBufferLength, IntByReference lpcchReturnLength ); public int GetLastError(); }
Testing:
import java.util.Arrays; import com.sun.jna.Native; import com.sun.jna.WString; import com.sun.jna.platform.win32.Win32Exception; import com.sun.jna.platform.win32.WinDef.DWORD; import com.sun.jna.ptr.IntByReference; public class TestJNA { static Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32.dll", Kernel32.class); public static void main(String[] args) { try { System.out.println(getPathNames()); } catch (Exception e) {
Result:
[C, :, \, , ]
To double-check it, at a DOS prompt, type: mountvol
Edited: To improve the value of the result ...
Change the return value of the getPathNames() method from:
return Arrays.toString(pathNames);
to
return new String(pathNames);
In my test application, you can simply:
String[] points = getPathNames().split("\u0000"); //split by Unicode NULL for(String s: points) System.out.println("mount: " + s);
My only problem is how JNA processes Unicode strings with lpszVolumePathNames NULL characters with the lpszVolumePathNames parameter in the Kernel32 GetVolumePathNamesForVolumeNameW() method, because:
lpszVolumePathNames [out]
Pointer to a buffer that receives a list of letters and disk space. GUID paths. The list is an array of null-terminated strings, terminated by an optional NULL character. If the buffer is not large enough to make a complete list, the buffer is stored as much of the list as possible.
Although, the JNI spec says (I'm not sure about the JNA side of things):
10.8 Completing Unicode Strings
Unicode strings received from GetStringChars or GetStringCritical do not end with NULL. Call GetStringLength to find out the number of 16-bit Unicode characters in a string. Some operating systems, such as Windows NT, expect two zero byte values ββto complete a Unicode string. You cannot pass the result of GetStringChars to the Windows NT APIs that expect a Unicode string. You must make another copy of the string and paste the value of the two trailing zero bytes.
http://java.sun.com/docs/books/jni/html/pitfalls.html
Edited by:
My code seems to be in order, as the lpszVolumePathNames parameter returns Unicode strings in zero correctly, checking for the presence of the string "\ u0000" in it:
String point = getPathNames().replaceAll("\u0000", "-");