我已經成功地使用JNA調用幾個Windows API函數,但我得到停留在這一個JNA的Windows API函數GetVolumePathNamesForVolumeName
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;
}
與這一個會發生什麼情況是,第一個值出來很好,但一個後可以看到有編碼這表明,我已經得到了偏移問題錯誤。
GetVolumePathNamesForVolumeName有兩種類型:GetVolumePathNamesForVolumeNameW(統一)和GetVolumePathNamesForVolumeNameA(ANSI)。你能告訴我們你的Kernel32 JNA接口的GetVolumePathNamesForVolumeName嗎? – eee 2011-03-15 11:23:08
我編輯了Kernel32 JNA接口到我的問題。謝謝 – 2011-03-15 15:09:58