JNA struct Array

Source: Internet
Author: User

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!





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.