【问题标题】:JNA Parameter Problems: Invalid memory AccessJNA 参数问题:无效的内存访问
【发布时间】:2019-01-29 08:12:01
【问题描述】:

我想通过 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);

我已经用 hust 一个字节[] 参数调用了一个方法。一切正常。但是现在当我执行 cbdpFuncs.mytestFunction 时,我得到“java.lang.Error: Invalid memory access”。我想我处理了参数 output_buffer 和 output_length false。但我不知道什么是正确的方法。

编辑: 在 ICdpClient mytestFunction 中调用如下:

 int mytestFunction (int[] input, SizeT input_length, Pointer output_buffer, SizeT output_length);

【问题讨论】:

  • IcbdpClient 定义mytestFunction 可能会有所帮助。
  • 感谢您的回复,请参阅 EdIT,IcbdpClient 只是一个接口

标签: java c++ memory jna


【解决方案1】:

我找到了解决方案:问题是我声明了 output_buffer 和 output_length false:这是正确的形式:

PointerByReference output_buffer = new PointerByReference();
IntByReference output_length = new IntByReference();

如果你想在那之后使用指针,请看这里:https://github.com/java-native-access/jna/blob/master/www/ByRefArguments.md

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多