使用JNA将数组从java传递到dll函数

时间:2014-06-07 00:35:23

标签: java c dll wrapper jna

我想将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指向正确的记忆位置。

1 个答案:

答案 0 :(得分:1)

您的printf格式存在问题:%d适用于整数。请改为%f