how to receive int array by pass a int pointer from java using JNA?

18 Views Asked by At

I'm currently working on a project which requires developing a Java application to access a native DLL (in C). I have chosen JNA for the bridging job and I am facing problems passing correct int pointer parameter from Java when the parameter is defined as int* . when I call a C function in .dll file :

//hisgrm is a {segNum} length integer array as return value;
int LA_histogram(float* dat, int datLen, int segNum, float* segVal, int* hisgrm);

And my Library interface like :

    int LA_histogram(float[] dat, int datLen, int segNum, Pointer segVal, Pointer hisgrm);

my call statement:

···
Pointer hisgrm = new Memory(15L * Native.getNativeSize(int.class));
int code = LA_histogram(safeDataFloat, safeDataFloat.length, segNum, segVal, hisgrm);
int[] integers1 = hisgrm.getIntArray(0, 15);
···

And every time I call this function with same parameters,I get different hisgrm as return value,make sure that this function only one result with same parameter by python program test with ctypes library.

I have tried using com.sun.jna.Pointer as :

Pointer hisgrm = new Memory(15L * Native.getNativeSize(int.class));
int code = LA_histogram(safeDataFloat, safeDataFloat.length, segNum, segVal, hisgrm);
int[] integers1 = hisgrm.getIntArray(0, 15);

,but it didn't work.

and my py code as :

hisgrmType = ctypes.c_int * segNum
hisgrmBuf = hisgrmType()

return_code = api.LA_histogram(
    ctypes.byref(dat),
    ctypes.c_int(data_len),
    ctypes.c_int(segNum),
    ctypes.byref(segValBuf),
    ctypes.byref(hisgrmBuf),
)

the python program receive correct hisgrmBuf.

So how to modify java code to recevice correct hisgrm?

0

There are 0 best solutions below