This article describes how to simulate a struct using JNA and pass the struct array as a parameter to the corresponding method.
The C language struct is defined as follows:
typedef struct Rect{int top;int bottom;int left;int right;} RECT;
JNA simulates the struct:
Need to introduce:
Import com. Sun. JNA .*;
Import com. Sun. JNA. PTR .*;
// The Order of the public fields in the public static class rect extends Structure {// structure subclass must be the same as that in the C language; otherwise, an error is reported! Public int top; Public int bottom; Public int left; Public int right; public static class byreference extends rect implements structure. byreference {} public static class byvalue extends rect implements structure. byvalue {}@ override protected list getfieldorder () {return arrays. aslist (New String [] {"TOP", "bottom", "Left", "right "});}}How can I transfer a struct array object to a method?
C language functions:
// Rects: struct array, Len: array length void function (rect * rects, int Len );
JNA simulation is as follows:
void function(Rect[] rects,int len);
The call method is as follows:
Int Len = 5; // define the array rect [] array = (rect []) New rect (). toarray (LEN); function (array, Len );
In fact, this mainly refers to the creation of struct arrays. Note: if the following method is used to create a struct array, an empty array will be generated and the requested space will not be available, currently, I do not know why I cannot apply for space.
int len = 5;Rect[] array = new Rect[len];
After this code is executed, array = NULL, that is, an error occurred while creating the array!
Hope experts can help answer this question!