我想将Java数组作为参数传递给c dll抛出JNA, 这是我的代码:
import com.sun.jna.*;
public class Javatest {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
"test", CLibrary.class);
void test(Pointer p,int width);
}
public static void main(String[] args) {
Pointer p = new Memory(5*Native.getNativeSize(Double.TYPE));
for (int i = 0; i < 5; i++) {
p.setDouble(i*Native.getNativeSize(Double.TYPE),5);
}
CLibrary.INSTANCE.test(p,5);
}
}
C代码:
#include <stdio.h>
__declspec(dllexport) int test(double *a,int width){
for(int i =0 ; i<width;i++){
printf("%d",a[i]);
}
return 0;
}
结果:00000
看起来Pointer doest指向正确的记忆位置。
答案 0 :(得分:1)
您的printf
格式存在问题:%d
适用于整数。请改为%f
。