1
votes

I have googled for a long time, and nothing helps.

EDIT: This is the code after implementing technomage's advice.

Example struct C++ code:

typedef struct _SOMESTRUCT{
    const char* String1;
    const char* String2;
} SOMESTRUCT, *LPSOMESTRUCT;

Example C++ function that "fills up" the struct with data:

int GetSomeStruct(_SOMESTRUCT somes);

Translation of struct to java:

public class SomeStruct extends Structure{
    public String String1;
    public String String2;
    public class ByValue extends SomeStruct implements Structure.ByValue{}
    public SomeStruct(Pointer p){ //constructors of struct
        super(p);
        read();
    }
    public SomeStruct(){
         super();
         read();
    }
}

Translation of function to java method:

int GetSomeStruct(SomeStruct.ByValue structref);

How I execute the code from main Java app:

EnclosingClass.SomeStruct sstruct = enclosingInstance.new SomeStruct();
EnclosingClass.SomeStruc.ByValue sstructval = sstruct.new ByValue();
enclosingInstanceofClasswithTranslatedCfunctions.GetSomeStruct(sstructref);

Assumptions:

  • I have correctly executed Native.loadLibrary before attempting to use the function. (simpler functions that return ints work fine as does a callback function for receiving event notifications)
  • The java translations are both in separate files (as in the c code) nested in public classes that extend JNA library.
  • The int that is returned by GetSomeStruct is zero if everything executes well on the c side. I keep getting zero.
1

1 Answers

1
votes

Your native declaration passes a struct by value. The following declarations are equivalent:

int GetSomeStruct(struct _SOMESTRUCT somes);
int GetSomeStruct(SOMESTRUCT somes);

You need to define your Java mapping to explicitly accept an argument which implements the Structure.ByValue interface. JNA assumes struct* by default, so you need the explicit ByValue usage to indicate you want something different.

public class MyStruct extends Structure {
    public class ByValue extends MyStruct implements Structure.ByValue { }
}

SOMESTRUCT is a typedef of struct _SOMESTRUCT, which is the most fundamental way of referring to that struct. Both are type specifiers, and may be used interchangeably. _SOMESTRUCT is often called the "tag" for the structure definition it is preceding.