6 messages in net.java.dev.jna.usersRe: [jna-users] need for full definit...
FromSent OnAttachments
Schraagen, MarijnOct 24, 2008 10:26 am 
LYou...@gkservices.comOct 24, 2008 11:34 am 
Timothy WallOct 24, 2008 11:42 am 
Luigi BitontiOct 25, 2008 4:30 am 
Timothy WallOct 25, 2008 1:26 pm 
Marijn SchraagenOct 27, 2008 9:08 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] need for full definition of nested structs?Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Oct 24, 2008 11:42:25 am
List:net.java.dev.jna.users

If you know the size of the part of the structure which is of no interest, you can simply declare it as a byte[] of the appropriate size, e.g.

class MyStruct extends Structure { public int myfield; public byte[] ignore = new byte[1024]; public int moreFields; }

On Oct 24, 2008, at 1:27 PM, Schraagen, Marijn wrote:

Good (European) evening,

I have a question about nested structs. I have searched the mailing list archives and the jna documentation but could not find the answer to this issue; I apologize if it has been discussed and I have missed it somehow.

The issue is closely related to the 'Nested Structure Definitions' section on https://jna.dev.java.net/javadoc/overview-summary.html. I have set up a small testing scenario with a dll containing the following structs and a function using them. The function calculates the square of the argument and adds 1.

typedef struct { double base1; double square1; } SQBS;

typedef struct { SQBS basesquare; double res; } SQRTP1;

DLLIMPORT void square_plus1v2(SQRTP1 *structuur, double arg){ structuur->basesquare.base1 = arg; structuur->basesquare.square1 = arg * arg; structuur->res = structuur->basesquare.square1 + 1; }

This is mapped in Java using the jna to the following:

public interface dllproject extends Library{ public static class SQBS extends Structure{ public double base1; public double square1; }

public static class SQRTP1 extends Structure{ public SQBK basesquare; public double res; }

void square_plus1v2(SQRTP1 structuur, double arg); }

and function call

dllproject lib = (dllproject) Native.loadLibrary("dllproject",dllproject.class); dllproject.SQRTP1 sq2 = new dllproject.SQRTP1(); lib.square_plus1v2(sq2, 6); System.out.println("Result: "+sq2.res);

This works perfectly fine.

However, here I have declared the nested struct SQBS with its own Structure. This struct is basically of no relevance to my Java program, where I only need to know the result (SQRTP1.res) variable. However, without the SQBS definition the program stops working correctly: now the value of SQBS.base1 is returned when asked for SQRTP1.res (this behaviour is documented by various sources).

Obviously, in this example it is no problem to declare the extra SQBS structure, and moreover, the functionality of the example could be implemented without nested structures or even without any structures at all. However, this is a test case: the end goal is to use a (3rd party) dll with a great amount of deeply nested structs in a hierarchy of header files. I need only the top-level functionality, and I wonder if it is neccesary to mirror the full definition of structs in my Java program.

Thank you in advance for any help or comments on this issue.

Kind regards,

Marijn Schraagen