JNA is called with a struct array as a parameter:
Java code
- C++
- Student definition of structural body
- typedef struct
- {
- int age;
- Char name[20];
- }student;
- Modifying the property values of a Java object
- JNAAPI bool Changeobjs (Student stu[],int size)
- {
- For (int i=0;i<size;i++)
- {
- stu[i].age*=10;
- strcpy (Stu[i].name,"Wokettas");
- }
- return true;
- }
- Java
- Jna Calling Method
- Student[] stus=new students[2];
- Student s1=New Student ();
- Student s2=New Student ();
- s1.age=1;
- s1.name=arrays.copyof ("K1". GetBytes (), 20);
- S2.age=2;
- S2.name=arrays.copyof ("K2". GetBytes (),20);
- stus[0]=s1; //Array assignment
- stus[1]=s2;
- Gui.gui.changeObjs (Stus, stus.length);
Operation Error:
Structure array elements must use contiguous memory (bad backing address at Structure array index 1)
The structure array must use contiguous memory regions, and the above example s1,s2 are new objects that are not contiguous; That is, the traditional way to initialize the array can not be resolved, query JNA API Discovery provides:
ToArray
ToArray (int size)
Returns A view of this structure ' s memory as an array of structures. Note that this Structure must has a public, No-arg constructor. IF The structure is currently using a Memory backing, the memory would be resized to fit the entire array.
The Modified code:
Java code
- Array of Test objects
- Student student=New Student ();
- Student[] stus= (student[]) Student.toarray (2); //Allocate a struct array of size 2
- stus[0].age=1;
- stus[0].name=arrays.copyof ("K1". GetBytes (), 20);
- stus[1].age=2;
- stus[1].name=arrays.copyof ("K2". GetBytes (),20);
- Gui.gui.changeObjs (Stus, stus.length);
http://tcspecial.iteye.com/blog/1665583//Original address
JNA struct parameter passing, Java array