JNA parameter passing problem, Java array

Source: Internet
Author: User
Tags array length

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

This paper mainly describes the use of JNA to simulate a struct and pass a struct array as an argument to the corresponding method.

The C language structure is defined as follows:

[CPP]View PlainCopy 
    1. typedef struct Rect
    2. {
    3. int top;
    4. int bottom;
    5. int left;
    6. int right;
    7. } RECT;

JNA simulates the structure:

Need to introduce:

Import com.sun.jna.*;
Import com.sun.jna.ptr.*;

[Java]View PlainCopy  
  1. Rect struct BODY
  2. Public static class Rect extends Structure {
  3. The order of the public fields in the//structure subclass must match the order of the structures in the C language, otherwise it will be an error!
  4. public int top;
  5. public int bottom;
  6. public int left;
  7. public int right;
  8. public static class Byreference extends Rect implements Structure.byreference {}
  9. public static class Byvalue extends Rect implements Structure.byvalue {}
  10. @Override
  11. protected List Getfieldorder () {
  12. return Arrays.aslist (new string[]{"Top", "bottom", "left", "right"});
  13. }
  14. }
Now to pass a struct array object to the method, how do you do it?

C Language Functions:

[CPP]View PlainCopy 
    1. Rects: struct array, len: array length
    2. void function (rect* rects, int len);

The JNA simulation is as follows:

[Java]View PlainCopy  
    1. void function (rect[] rects,int len);

Create an array and invoke the following method:

[Java]View PlainCopy 
    1. int len = 5;
    2. Defining arrays
    3. rect[] Array = (rect[])new Rect (). ToArray (len);
    4. function (array, len);

Note that the creation of an array here uses the JNA ToArray () method instead of the Java general method of creating an array because the memory space is discontinuous in Java, and the JNA definition array is required to use the ToArray method so that the instantiated array memory space is contiguous.

In fact, here is mainly the creation of the structure of the array of places to note that if created using the following way, it will produce an empty array, the request is not space:

[Java]View PlainCopy  
    1. int len = 5;
    2. rect[] Array = new Rect[len];
After this code executes, array=null, that is, the creation of the array failed!

Cause: Memory space in Java is discontinuous, JNA definition array is required to use the ToArray method, so that the instantiation of the array memory space is continuous, otherwise the first piece of data is correct, the other is misplaced.

See another article: http://tcspecial.iteye.com/blog/1665583

http://blog.csdn.net/zht666/article/details/38658985//Original address

JNA parameter passing problem, Java array

Related Article

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.