[英]Passing array from java to dll function with JNA
I want to pass Java array as parameter to c dll throw JNA , here is my code : 我想将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 code : 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;
}
RESULT : 00000 结果:00000
Looks like the Pointer doest's points at the right memory place . 看起来Pointer不会将指针指向正确的存储位置。
There's a problem with your printf
format: %d
is for integers. 您的
printf
格式存在问题: %d
用于整数。 Try %f
instead. 请尝试使用
%f
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.