[英]JNA Parameter Problems: Invalid memory Access
我想通過JNA在Java中調用函數“ mytestFunction”。 這就是我在C ++中調用方法的方式:
size_t input_length = 67U;
uint8_t input[67U] = { 0x30, 0x41, 0x80, ....};
uint8_t *output_buffer = NULL;
size_t output_length = 0;
uint32_t errorCode = 0;
errorCode = mytestFunction(input, input_length, &output_buffer, &output_length);
這就是我用Java調用方法的方式:
IcbdpClient cbdpFuncs = (IcbdpClient) Native.load("myDLL.dll", IcbdpClient.class);
int[] input = new int[]{0x30, 0x41, 0x80, ....};
SizeT input_length = new SizeT(input.length);
Pointer output_buffer = new Memory(1);
SizeT output_length = new SizeT(0);
err = cbdpFuncs.mytestFunction(input, input_length, output_buffer, output_length);
我已經調用了一個只有一個byte []參數的方法。 一切正常。 但是現在當我執行cbdpFuncs.mytestFunction時,我得到“ java.lang.Error:無效的內存訪問”。 我認為我處理了參數false output_buffer和output_length。 但是我不知道什么是正確的方法。
編輯:在ICdpClient中,mytestFunction的調用方式如下:
int mytestFunction (int[] input, SizeT input_length, Pointer output_buffer, SizeT output_length);
我找到了解決方案:問題是我聲明了output_buffer和output_length為false:這是正確的形式:
PointerByReference output_buffer = new PointerByReference();
IntByReference output_length = new IntByReference();
如果您之后要使用Pointer,請參見此處: https : //github.com/java-native-access/jna/blob/master/www/ByRefArguments.md
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.