2 messages in net.java.dev.jna.usersRe: [jna-users] getting a table of st...
FromSent OnAttachments
sylv...@free.frDec 21, 2007 4:21 am 
Timothy WallDec 21, 2007 6:59 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] getting a table of structures filled by C back to JavaActions...
From:Timothy Wall (twal@dev.java.net)
Date:Dec 21, 2007 6:59:41 am
List:net.java.dev.jna.users

On Dec 21, 2007, at 7:21 AM, sylv@free.fr wrote:

Hi !

I am discovering JNA, and I must say it looks to be really great ! I have a small problem however. I am calling this C function from Java:

int GcGetPCFDirVar(PCF_DESCR * pcf, VARIO_DIR_PARAM * vario_dir_param, VARIO_LAG_INFO **lag_info_main, VARIO_LAG_INFO **lag_info_co, VARIO_LAG_INFO **lag_info_cross)

If the last two parameters are meant to be outputs, then you should use a PointerByReference to obtain the result, e.g.

PointerByReference pref = new PointerByReference(); // for lag_info_co // native call here Pointer p = pref.getValue(); // this is the same as *lag_info_co VARIO_LAG_INFO vli = new VARIO_LAG_INFO(); vli.useMemory(p);

Is the pointer a block of contiguous structures? If so, how do you know how many?

PCF_DESCR and VARIO_DIR_PARAM are structures used as inputs. lag_info_main, lag_info_co and lag_info_cross are tables of structures returned by GcGetPCFDirVar. Here is how a VARIO_LAG_INFO is initialized in Java.

import com.sun.jna.Structure; public class VARIO_LAG_INFO extends Structure {

public static class ByReference extends VARIO_LAG_INFO implements Structure.ByReference { }

public long nb_pairs; public float dist; public float covariance; public float semi_vario; public float rho; public float mean_head; public float mean_tail; public float var_head; public float var_tail;

public VARIO_LAG_INFO(){ write(); } }

Newly-allocated structure memory is cleared by default, so the "write" here is redundant and probably misleading.

My problem is that I can not find a way to get back the output values from lag_info_main in Java. I put some fprintf (!) in the C source supposed to fill the VARIO_LAG_INFO[] tables of structures. Everything works perfectly on the C side: values like nb_pairs or semi_vario are correctly computed, but back in Java I get only zeros for all the fields. I also checked that the fields of the input structures (like PCF_DESCR) were ok when being read in the C: everything's fine.

Note that Structure.ByReference indicates that a Structure should be treated as a pointer when it appears as a field within a Structure, as an array field within a Structure, or as an array argument/return value.

An array of Structure.ByReference as an argument is equivalent to an array of pointer. Is that what you want?

I suppose my problems come from the way I initialise / get back the VARIO_LAG_INFO table. Here is the last try I wrote in Java:

VARIO_LAG_INFO.ByReference vli_main = new VARIO_LAG_INFO.ByReference(); VARIO_LAG_INFO.ByReference vli_co = new VARIO_LAG_INFO.ByReference(); VARIO_LAG_INFO.ByReference vli_cross = new VARIO_LAG_INFO.ByReference(); VARIO_LAG_INFO.ByReference[] vlis_main = (VARIO_LAG_INFO.ByReference[])vli_main.toArray(nbLag); VARIO_LAG_INFO.ByReference[] vlis_co = (VARIO_LAG_INFO.ByReference[])vli_co.toArray(nbLag); VARIO_LAG_INFO.ByReference[] vlis_cross = (VARIO_LAG_INFO.ByReference[])vli_cross.toArray(nbLag);

int retour = GcAPI.INSTANCE.GcGetPCFDirVar(pdescr, vdp, vlis_main[0].getPointer(), vlis_co[0].getPointer(), vlis_cross [0].getPointer()); System.err.println("\nRetour GcAPI: " + retour);

for (int i=0;i<nbLag;i++){ vlis_main[i].read(); System.err.println("NbPair[" + i + "] = " + vlis_main[i].dist); // here I get zeros (but this value is != 0 and correct in C !) }

Any idea to get back correct values in my table of VARIO_LAG_INFO is welcome. Thank you so much in advance.