2

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 .

Grg_Lnt
  • 61
  • 10

1 Answers1

1

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

jdigital
  • 11,926
  • 4
  • 34
  • 51