简体   繁体   English

如何使用 JNA 将 java 原始数组传递给 C dll?

[英]How to pass java primitive arrays to a C dll using JNA?

When mapping java code to a DLL using JNA, how should one pass/use a java array ( int[] , double[] ) in a C call when the C's function argument is a pointer?使用 JNA 将 java 代码映射到 DLL 时,当 C 的函数参数是指针时,应该如何在 C 调用中传递/使用 java 数组( int[]double[] )? I am facing a bug, and I think it is somewhere in my mapping of arrays to C.我正面临一个错误,我认为它在我将数组映射到 C 的某个地方。


What I tried: and the bug that I am trying to fix我尝试过的:以及我正在尝试修复的错误

For my project I need to restructure the codebase behind clp-java .对于我的项目,我需要重构clp-java背后的代码库。 In doing so, I have a C header file with the following function which add constraints for a LP problem ( for example: 2.25*x1 - 3.3*x2 =4 ).在这样做时,我有一个带有以下函数的 C 头文件,它为 LP 问题添加约束(例如: 2.25*x1 - 3.3*x2 =4 )。

CLPLIB_EXPORT void CLP_LINKAGE Clp_addRows(Clp_Simplex *model, int number,
const double *rowLower, const double *rowUpper,
const CoinBigIndex *rowStarts, const int *columns,
const double *elements);

In java I have rowLower , rowUpper , rowStarts , columnscolumns and elements as java arrays (either int[] of double[] ).在 java 中,我有rowLowerrowUpperrowStartscolumnscolumnselements作为 java 数组( int[]double[] )。 clp-java uses BridJ, where the function above is called via clp-java使用 BridJ,上面的函数是通过调用

CLPNative.clpAddRows(pointerToModel, number, 
        Pointer.pointerToDoubles(rowLower), 
        Pointer.pointerToDoubles(rowUpper), 
        Pointer.pointerToInts(rowStarts), 
        Pointer.pointerToInts(columnscolumns), 
        Pointer.pointerToDoubles(elements));

Using plain JNA, the JNA documentation states that arrays map to pointers, such that a call to the C function would be:使用普通 JNA, JNA 文档指出数组映射到指针,因此对 C 函数的调用将是:

CLPNative.clpAddRows(pointerToModel, number, 
        rowLower, rowUpper, rowStarts, columnscolumns, elements);

Unfortunately, when I pass the same arrays to both methods and retrieve the data in memory, I get different answers for the second variable in the constraint (first one is ok): BridJ yields -3.3, my JNA method outputs 1.777E-307.不幸的是,当我将相同的数组传递给两种方法并检索内存中的数据时,我得到了约束中第二个变量的不同答案(第一个可以):BridJ 产生 -3.3,我的 JNA 方法输出 1.777E-307。 Same DLL, same machine (Java 11).相同的 DLL,相同的机器(Java 11)。

On the internet I found this example , which maps an array in Java to a JNA pointer and passes this pointer to the C function.我在网上找到了这个例子,它把 Java 中的一个数组映射到一个 JNA 指针,并将这个指针传递给 C 函数。 This I tried using:我尝试使用:

private Pointer intArrayToPointer(int[] pArray) {
    Pointer pointerToArray = new Memory(pArray.length*Native.getNativeSize(Integer.TYPE));
    for (int i=0; i< pArray.length; i++) {
      pointerToArray.setInt(i, pArray[i]);
    }
    return pointerToArray;
  }

Though if I use this in my JNA function call (and change the JNA interface accordingly), I get a Java error "invalid memory access".虽然如果我在 JNA 函数调用中使用它(并相应地更改 JNA 接口),我会收到一个 Java 错误“无效的内存访问”。 Fixing this based on this StackOverflow Q/A (the offset in setInt() needs to be shifted by Native.getNativeSize(Integer.TYPE) , reveals the same erroneous output (coefficient for x2 is 1.777E-307 instead of -3.3)基于此 StackOverflow Q/A修复此问题setInt()的偏移量需要由Native.getNativeSize(Integer.TYPE) ,显示相同的错误输出(x2 的系数为 1.777E-307 而不是 -3.3)

CLPNative.clpAddRows(pointerToModel, number, 
        doubleArrayToPointer(rowLower),....);

Extra info: I check the coefficients with the following function/method:额外信息:我使用以下函数/方法检查系数:

  public double getNativeConstraintCoefficient(CLPConstraint pConstraint, CLPVariable pVariable) {
    <... some magic to compute the position...>
     Pounter<Double> elements = CLPNative.Clp_GetElements(pointerToModel);
     return elements.getDoubleAtIndex(pos-1); // using BridJ
     return elements.getDouble(pos - 1)  // using JNA
}

Found it, it is the way I read the data, not the way I write it.找到了,是我读数据的方式,不是我写的方式。

BridJ is a fancy wrapper, which makes sure you read a pointer at indices that correspond to doubles (on my machine that is 8 bits). BridJ 是一个花哨的包装器,它确保您读取与双精度对应的索引处的指针(在我的机器上是 8 位)。 In other words: index=0 reads bits 1-8 bits, index=2 reads bits 9-16.换句话说:index=0 读取位 1-8 位,index=2 读取位 9-16。

JNA is not 'so fancy'. JNA 并不“那么花哨”。 Pointer.getDouble(offset) reads a double (8-bit value) starting from the address it points to. Pointer.getDouble(offset) 从它指向的地址开始读取一个双精度值(8 位值)。 offset=0 reads bits 1-8, and offset=1 reads 2-9. offset=0 读取第 1-8 位,offset=1 读取第 2-9 位。 To prevent this, one needs to multiply the offset by the datatype you are looking for为了防止这种情况,需要将偏移量乘以您要查找的数据类型

Pointer.getDouble(offset*Native.getNativeSize(Double.TYPE))

In your answer you note that the BridJ indices correspond to the array, such that each index is a new double , and correctly note that if you're only fetching a single double from a JNA pointer, you need to use a byte offset, indicating that's "not as fancy".您的回答中,您注意到 BridJ 索引对应于数组,因此每个索引都是一个新的double ,并且正确地注意,如果您只从 JNA 指针中获取单个double ,则需要使用字节偏移量,表示那是“不那么花哨”。

However, JNA can be just "fancy" if you want it to be.但是,如果您愿意,JNA 可以只是“花哨的”。 Simply use this:只需使用这个:

Pointer.getDoubleArray(offset, arraySize);

For example:例如:

int arraySize = pArray.length; // I think, based on your code
double[] foo = elements.getDoubleArray(0, arraySize);
return foo[pos - 1];

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

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