Passing array from java to dll function with JNA

为君一笑 提交于 2021-02-19 07:21:29

问题


I want to pass Java array as parameter to c dll throw JNA , here is my code :

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 :

#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

Looks like the Pointer doest's points at the right memory place .


回答1:


There's a problem with your printf format: %d is for integers. Try %f instead.



来源:https://stackoverflow.com/questions/24092495/passing-array-from-java-to-dll-function-with-jna

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!