Re: Problem with passing array to a JNA pointer struct member

343 views
Skip to first unread message

Timothy Wall

unread,
Sep 6, 2016, 11:44:31 AM9/6/16
to jna-...@googlegroups.com
The field has to be of type Structure.ByReference so that a pointer value is used rather than inlining the structure.

Structure.toArray() will provide you with a block of contiguously allocated structures (that method shouldn't care about the ByReference tag).  Assigning the field the first element of this array should be sufficient.  If JNA is reading the structure from a raw pointer, though, it won't know that the field is an array and will only synch a single element.

JNA will auto-synch the memory in Structure.read()/Structure.write() for the entire array.

On Mon, Sep 5, 2016 at 4:51 PM, cap86 <kaila....@gmail.com> wrote:
Hi,

I've got a couple of C Structs in a third party lib that are written like this:

typedef struct
{
 
double   tenor;
 
double    rate;
} TERMSTRUC;


typedef struct
{
       TERMSTRUC  
*curve;
       
short     count;
 
const char *name;
 
short     flag;
 
char   *conv;
} NAMEDTERMCURVE;


The JNA equivalents I've written are below. My question is - how would I pass an array of TERMSTRUC[] to the NAMEDTERMCURVE?

I can initiate the a new price data curve map of 5 elements:

NAMEDTERMCURVE ntc = new NAMEDTERMCURVE();
NAMEDTERMCURVE[] priceData = (NAMEDTERMCURVE[])ntc.toArray(5);

Lets say I have a termstruc to assign to the "curve" pointer - how do I do that using a Memory or Pointer?

TERMSTRUC[] test = something;
priceData[0].curve = test (obviously this doesn't work or compile at the moment but how would I assign an array of type TERMSTRUC[] to the curve member?)


 public static class NAMEDTERMCURVE extends Structure
 
{
 
public Pointer curve;
 
public short count;
 
public String name;
 
public short flag;
 
public Pointer conv;


 
public static class ByReference extends NAMEDTERMCURVE implements Structure.ByReference
 
{
 
public ByReference() { }
 
public ByReference(Pointer p){ super(p); read(); }
 
}
 
public static class ByValue extends NAMEDTERMCURVE implements Structure.ByValue { }
 
public NAMEDTERMCURVE(){}
 
public NAMEDTERMCURVE(Pointer p){super(p); read();}
 
public NAMEDTERMCURVE(
 
Pointer curve,
 
short count,
 
String name,
 
short flag,
 
Pointer conv)
 
{
 
this.curve = curve;
 
this.count = count;
 
this.name = name;
 
this.flag = flag;
 
this.conv = conv;
 
}


 
@Override
 
protected List getFieldOrder()
 
{
 
return Arrays.asList(new String[] { "curve", "count", "name", "flag", "conv" } );
 
}
 
}




 public static class TERMSTRUC extends Structure
 
{
 
public double tenor;
 
public double rate;


 
public static class ByReference extends TERMSTRUC implements Structure.ByReference
 
{
 
public ByReference() { }
 
public ByReference(Pointer p){ super(p); read(); }
 
}
 
public static class ByValue extends TERMSTRUC implements Structure.ByValue { }


 
public TERMSTRUC(){}
 
public TERMSTRUC(Pointer p) { super(p); read(); }
 
public TERMSTRUC(double tenor, double rate)
 
{
 
this.tenor = tenor;
 
this.rate = rate;
 
}


 
@Override
 
protected List<String> getFieldOrder()
 
{
 
return Arrays.asList(new String[]{"tenor", "rate"});
 
}
 
}

--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Timothy Wall

unread,
Sep 7, 2016, 2:48:26 PM9/7/16
to jna-...@googlegroups.com
Common usage like thus:

    public class TERMSTRUC extends Structure {
        public static class ByReference extends MyStructure implements Structure.ByReference { 
            public ByReference() { }
            public ByReference(Pointer p) { super(p); read(); }
        }
    }

Then declare the "curve" field to be of type TERMSTRUC.ByReference, and create your curve array with that type as well.



On Wed, Sep 7, 2016 at 4:26 AM, cap86 <kaila....@gmail.com> wrote:
Hi Tim,

Can you show me an example of what you mean when you indicate to define the field of type Structure.ByReference? The "curve" field in the NAMEDTERMCURVE Java struct is of type Pointer. If I change that to Structure.ByReference - what would I assign namedTermCurve[0].curve below to?

public static void main(String[] args) throws Exception 
{
TERMSTRUC ts = new TERMSTRUC();
TERMSTRUC[] termStruc = (TERMSTRUC[])ts.toArray(1);
termStruc[0].tenor = 42643;
termStruc[0].rate = 25654.00;

/* namedtermcurve struct with 2 term struc's */
NAMEDTERMCURVE ntc = new NAMEDTERMCURVE();
NAMEDTERMCURVE[] namedTermCurve = (NAMEDTERMCURVE[])ntc.toArray(1);

namedTermCurve[0].curve = termStruc;
namedTermCurve[0].count = (short)1; 

}

To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages