我已经成功地使用JNA调用了几个Windows API函数,但是我陷入了这个困境
GetVolumePathNamesForVolumeName
完整的C声明是:
BOOL WINAPI GetVolumePathNamesForVolumeName(
__in LPCTSTR lpszVolumeName,
__out LPTSTR lpszVolumePathNames,
__in DWORD cchBufferLength,
__out PDWORD lpcchReturnLength
);
我的Kernel32接口方法原型是:
boolean GetVolumePathNamesForVolumeName(String lpszVolumeName, Pointer lpszVolumePathNames, int cchBufferLength, Pointer lpcchReturnLength);
我用下面的来加载界面
Native.loadLibrary('kernel32', Kernel32.class, W32APIOptions.UNICODE_OPTIONS)
我试过了:
public String[] getPathNames() {
Memory pathNames = new Memory(100);
Memory len = new Memory(4);
if (!kernel32.GetVolumePathNamesForVolumeName(this.getGuidPath(), pathNames, 100, len)) {
if (kernel32.GetLastError() == WindowsConstants.ERROR_MORE_DATA) {
pathNames = new Memory(len.getInt(0));
if (!kernel32.GetVolumePathNamesForVolumeName(this.getGuidPath(), pathNames, len.getInt(0), len)) {
throw new WinApiException(kernel32.GetLastError());
}
}
else
throw new WinApiException(kernel32.GetLastError());
}
int count = len.getInt(0);
return pathNames.getStringArray(0, true);
}
这根本不起作用。尚不确定该异常,但是我的代码炸了。
以下是这类作品:
public String[] getPathNames() {
Memory pathNames = new Memory(100);
Memory len = new Memory(4);
if (!kernel32.GetVolumePathNamesForVolumeName(this.getGuidPath(), pathNames, 100, len)) {
if (kernel32.GetLastError() == WindowsConstants.ERROR_MORE_DATA) {
pathNames = new Memory(len.getInt(0));
if (!kernel32.GetVolumePathNamesForVolumeName(this.getGuidPath(), pathNames, len.getInt(0), len)) {
throw new WinApiException(kernel32.GetLastError());
}
}
else
throw new WinApiException(kernel32.GetLastError());
}
int count = len.getInt(0);
String[] result = new String[count];
int offset = 0;
for (int i = 0; i < count; i++) {
result[i] = pathNames.getString(offset, true);
offset += result[i].length() * Native.WCHAR_SIZE + Native.WCHAR_SIZE;
}
return result;
}
这个发生的事情是第一个值很好,但是在那之后可以看到存在编码问题,这表明我错了偏移量。
我的版本\\?\Volume{5b57f944-8d60-11de-8b2a-806d6172696f}\
应该得到C:\
Kernel32接口:
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();
}
测试应用:
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);
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(getPathNames());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getPathNames() throws Win32Exception {
DWORD value = new DWORD(100);
char[] pathNames = new char[100];
IntByReference len = new IntByReference();
if (kernel32.GetVolumePathNamesForVolumeNameW(getGuidPath(), pathNames, value, len)) {
if (kernel32.GetLastError() == WindowsConstants.ERROR_MORE_DATA) {
pathNames = new char[len.getValue()];
DWORD sz = new DWORD(len.getValue());
if (!kernel32.GetVolumePathNamesForVolumeNameW(getGuidPath(), pathNames, sz, len)) {
throw new Win32Exception(kernel32.GetLastError());
}
}
else
throw new Win32Exception(kernel32.GetLastError());
}
return Arrays.toString(pathNames);
}
private static WString getGuidPath() {
final WString str = new WString("\\\\?\\Volume{5b57f944-8d60-11de-8b2a-806d6172696f}\\");
return str;
}
}
结果:
[C, :, \, , ]
要仔细检查,我在DOS命令提示符下键入: mountvol
编辑:为了提高结果值...
从以下位置更改getPathNames()
方法的返回值:
return Arrays.toString(pathNames);
至
return new String(pathNames);
在我的测试应用程序中,您可以:
String[] points = getPathNames().split("\u0000"); //split by Unicode NULL
for(String s: points) System.out.println("mount: " + s);
我唯一关心的是,JNA如何处理lpszVolumePathNames
Kernel32 GetVolumePathNamesForVolumeNameW()
方法中的参数中以NULL结尾的Unicode字符串,因为:
lpszVolumePathNames [输出]
指向缓冲区的指针,该缓冲区接收驱动器号和卷GUID路径的列表。该列表是一个由空终止的字符串组成的数组,这些字符串以其他NULL字符终止。如果缓冲区的大小不足以容纳完整列表,则缓冲区将保留尽可能多的列表。
虽然,JNI规范说(我不确定在JNA方面):
10.8终止Unicode字符串
从GetStringChars或GetStringCritical获得的Unicode字符串不是NULL终止的。调用GetStringLength找出字符串中16位Unicode字符的数量。某些操作系统,例如Windows NT,期望两个结尾的零字节值来终止Unicode字符串。您不能将GetStringChars的结果传递给需要Unicode字符串的Windows NT API。您必须复制该字符串,然后插入两个结尾的零字节值。
编辑:
似乎我的代码正常,因为该lpszVolumePathNames
参数通过验证其中是否存在“ \ u0000”字符串来正确返回Unicode中以NULL结尾的字符串:
String point = getPathNames().replaceAll("\u0000", "-");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句