[英]Passing array from java to dll function with JNA
我想将Java数组作为参数传递给c dll throw 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不会将指针指向正确的存储位置。
您的printf
格式存在问题: %d
用于整数。 请尝试使用%f
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.