2
我正在製作一個項目,其中涉及使用JNA將二維數組從Java傳遞到C++。但我在傳遞二維數組時遇到問題。我能夠毫無問題地傳遞一維數組。如何使用JNA將二維數組從Java傳遞到C
Java代碼 -
import com.sun.jna.Library;
import com.sun.jna.Native;
public class HelloWorld {
public static void main(String argv[]) {
CTest lib = CTest.ctest;
int a[][] = {{2, 3}, {2, 4}};
System.out.println(lib.helloFromC(a));
}
public interface CTest extends Library {
CTest ctest = (CTest) Native.loadLibrary("cpptest", CTest.class);
public int helloFromC(int x[][]);
}
}
C++代碼
#include <stdio.h>
extern "C" __declspec(dllexport)
int helloFromC(int a[][2]) {
int i, sum = 0, j;
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
printf("%d\n",a[i][j]);
sum += a[i][j];}
}
printf("%d\n",sum);
return sum;
}
當我編譯這兩個文件,我沒有錯誤,但是當我運行java文件,我得到,
Exception in thread "main" java.lang.IllegalArgumentException: Unsupported array
argument type: class [I
at com.sun.jna.Function.convertArgument(Function.java:609)
at com.sun.jna.Function.invoke(Function.java:297)
at com.sun.jna.Library$Handler.invoke(Library.java:212)
at $Proxy0.helloFromC(Unknown Source)
at HelloWorld.main(HelloWorld.java:8)
許多元素你確定'int a [] [2]'是一個有效的參數嗎? –
@SotiriosDelimanolis:不,我不確定..我第一次使用JNA – user3215014