问题
I have a structure that contains some char pointers in C:
struct inputsheet
{
char* TestCaseID[MAX_TEST_CASES];
char* Description[MAX_TEST_CASES];
};
I have a function that is returns array of object of structure:
struct inputsheet* getapi(char *docname);
Now I want to use this in Java. How can I handle this array of object of structure? I am able to handle a single object but not the array.
For single object my code in C is here:
public class str3 extends com.sun.jna.Structure implements com.sun.jna.Structure.ByReference {
public Pointer a1;
public Pointer b2;
public Pointer c3[]=new Pointer[10];
}
Accessing it:
str2 s2=CLibrary.INSTANCE.parseid(xmlFile1);
for(Pointer p1:s2.testCaseID) {
if(p1!=null)
{
System.out.println(p1.getString(0));
}
}
EDIT
\\c code
struct str3{
char *a;
char *b;
char *ab[10];
}
\\jna implementation
package parser;
import com.sun.jna.Pointer;
public class str3 extends com.sun.jna.Structure implements com.sun.jna.Structure.ByReference{
public Pointer a1;
public Pointer b2;
public Pointer c3[]=new Pointer[10];
}
\\calling it
import com.sun.jna.Native;
import com.sun.jna.Pointer;
class ab{
interface CLibrary extends Library{
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("chardll",
CLibrary.class);
str3 getStruct();
}
public static void main(String[] args) {
int size = 5;
str3 a=CLibrary.INSTANCE.getStruct();
str3[] ab=(str3[])a.toArray(size);
System.out.println(ab[0].a1.getString(0));
}
}
It is giving Garbage value as output,, where I have to update my code to get the right output..
回答1:
From the javadoc:
Returning an Array of struct
Declare the method as returning a Structure of the appropriate type, then invoke
Structure.toArray(int)
to convert to an array of initialized structures of the appropriate size. Note that yourStructure
class must have a no-args constructor, and you are responsible for freeing the returned memory if applicable in whatever way is appropriate for the called function.// Original C code struct Display* get_displays(int* pcount); void free_displays(struct Display* displays); // Equivalent JNA mapping Display get_displays(IntByReference pcount); void free_displays(Display[] displays); ... IntByReference pcount = new IntByReference(); Display d = lib.get_displays(pcount); Display[] displays = (Display[])d.toArray(pcount.getValue()); ... lib.free_displays(displays);
EDIT
Nominally, your structure would look like this (based on your native definition):
class inputsheet extends Structure {
public Pointer[] TestCaseID = new Pointer[MAX_TEST_CASES];
public Pointer[] Description = new Pointer[MAX_TEST_CASES];
}
public inputsheet getapi(String docname);
int size = ...; // whatever you do to figure out the size of your returned array
inputsheet sheet = INSTANCE.getapi("some-doc");
inputsheet[] sheets = (inputsheet[])sheet.toArray(size);
If the caller doesn't need to write to the inputsheet
fields, you should use String
instead of Pointer
.
来源:https://stackoverflow.com/questions/15430260/returning-and-accessing-array-of-object-of-structure-in-jna