简体   繁体   English

JAVA -JNA C函数修改双精度数组

[英]JAVA -JNA C function modify array of doubles

I'm having trouble passing an array of doubles from Java to a C function with JNA that modifies the values by reference to the array. 我在将双精度数组从Java传递到JNA的C函数时遇到麻烦,该函数通过引用该数组来修改值。

C example function: C示例函数:

void examplefunct(double* dd) //<-- this wants an array of 6 doubles

this function requires an array of 6 doubles, now I've tried using 这个函数需要一个由6个double组成的数组,现在我尝试使用

DoubleByReference[] jdd = new DoubleByReference[6];

in the JNA implementation but I'm failing at getting values from the array. 在JNA实现中,但我无法从数组中获取值。 Honestly I think my implementation is incorrect, a basic example would be much appreciated since I'm new to JNA 老实说,我认为我的实现是不正确的,因为我是JNA的新手,所以不胜感激一个基本示例

Thanks in advance! 提前致谢!

There are a few ways you can map this function: 有几种方法可以映射此功能:

public interface MyLibrary extends Library {
    void examplefunct(Pointer dd);
    void examplefunct(java.nio.Buffer dd);
    void examplefunct(double[] dd);
}

If you allocate an instance of com.sun.jna.Memory , you can use the various memory accessor functions to write doubles into the buffer. 如果分配com.sun.jna.Memory的实例,则可以使用各种内存访问器功能将双精度型写入缓冲区。

If you use a direct NIO ByteBuffer, you can use the second example. 如果使用直接NIO ByteBuffer,则可以使用第二个示例。

If you use a Java primitive array, then JNA performs the copy from Java to native memory for you. 如果使用Java基本数组,则JNA会为您执行从Java到本机内存的复制。

Each of these has different performance characteristics, so you should test within your environment if performance is of concern. 每一个都有不同的性能特征,因此,如果需要关注性能,则应在环境中进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM