JNA以结构体数组为参数进行调用:

  1. ////// C++
  2. // student 结构体定义
  3. typedef struct
  4. {
  5. int age;
  6. char name[20];
  7. }Student;
  8. // 修改java对象的属性值
  9. JNAAPI bool changeObjs(Student stu[],int size)
  10. {
  11. for(int i=0;i<size;i++)
  12. {
  13. stu[i].age*=10;
  14. strcpy(stu[i].name,"wokettas");
  15. }
  16. return true;
  17. }
  18. /////// Java
  19. // JNA调用方法
  20. Student[] stus=new Students[2];
  21. Student s1=new Student();
  22. Student s2=new Student();
  23. s1.age=1;
  24. s1.name=Arrays.copyOf("k1".getBytes(), 20);
  25. s2.age=2;
  26. s2.name=Arrays.copyOf("k2".getBytes(),20);
  27. stus[0]=s1; //数组赋值
  28. stus[1]=s2;
  29. Gui.gui.changeObjs(stus, stus.length);

运行报错:

Structure array elements must use contiguous memory (bad backing address at Structure array index 1)

结构体数组必须使用连续的内存区域, 上例s1,s2都是new出来的新对象,不可能连续; 也就是说传统方式的初始化数组不能解决, 查询JNA api发现里面提供了:

toArray

public Structure[] toArray(int size)

Returns a view of this structure's memory as an array of structures. Note that this Structure must have a public, no-arg constructor. If the structure is currently using a Memory backing, the memory will be resized to fit the entire array.

修改后的代码:

  1. // 测试对象数组
  2. Student student=new Student();
  3. Student[] stus=(Student[]) student.toArray(2); //分配大小为2的结构体数组
  4. stus[0].age=1;
  5. stus[0].name=Arrays.copyOf("k1".getBytes(), 20);
  6. stus[1].age=2;
  7. stus[1].name=Arrays.copyOf("k2".getBytes(),20);
  8. Gui.gui.changeObjs(stus, stus.length);

http://tcspecial.iteye.com/blog/1665583  //原文地址

JNA结构体参数传递,Java数组的更多相关文章

  1. c#中关于结构体和字节数组转化

    最近在使用结构体与字节数组转化来实现socket间数据传输.现在开始整理一下.对于Marshal可以查阅msdn,关于字节数组与结构体转代码如下: using System; using System ...

  2. C#结构体和字节数组的转换函数

    在通信过程中,一般我们都会操作到字节数组.特别是希望在不同语言编程进行操作的时候. 虽然C#提供了序列化的支持,不用字节数组也行.但操作字节数组肯定会碰到.   一般都会采用结构来表示字节数组.但结构 ...

  3. 【VS开发】【编程开发】【C/C++开发】结构体中的数组与指针的内存分配情况说明

    [VS开发][编程开发][C/C++开发]结构体中的数组与指针的内存分配情况说明 标签:[VS开发] [编程开发] 主要是疑惑在结构体定义的数组的内存空间与指针动态分配的内存空间,在地址上连续性.以及 ...

  4. c# 调用c++sdk时结构体与byte数组互转

    /// <summary> /// 由结构体转换为byte数组 /// </summary> public static byte[] StructureToByte<T ...

  5. c++与C# winform的消息通讯--(结构体与byte数组的使用)

    近期正在做一个蓝牙驱动的使用程序,其中有一块从c++发送数据到C#的部分,网上查了很多资料,大多都是介绍如何通过调用函数获取用户数据.并且在消息发送中,很少介绍如何发送一个结构体,并且结构体里面有 b ...

  6. C语言结构体中字符数组的问题

    第一个程序 #include <stdio.h> #include <string.h> typedef struct student { char name[10]; int ...

  7. C语言 Struct 结构体在 Java 中的体现

    大一整个学期完成了 C 语言的学习,大二就进入了Java 的学习. 和C语言一样,我们都会尝试写一个小小的学生管理系统什么的,学习过 C 语言同学知道,在管理系统中 Struct 结构体是个很好用的东 ...

  8. c动态分配结构体二维数组

    这个问题我纠结了蛮久了,因为前面一直忙(自己也懒了点),所以没有能好好研究这个.希望这篇文章能够帮助你们. #include <stdio.h> #include <stdlib.h ...

  9. c结构体里的数组与指针

    /* 訪问成员数组名事实上得到的是数组的相对地址.而訪问成员指针事实上是相对地址里的内容 */ struct buf_str { int length; char buf[0]; }; struct ...

随机推荐

  1. OpenCV中图像指针注意点

    1.cvQueryFrame方法从摄像头或文件中抓取的帧图像是不能被释放和修改的 2.不要用delete删除,一定要用cvReleaseImage删除且要带有&符号.

  2. TCommThread -- 在delphi线程中实现消息循环

    http://www.techques.com/question/1-4073197/How-do-I-send-and-handle-message-between-TService-parent- ...

  3. How can I terminate a thread that has a seperate message loop?

    http://www.techques.com/question/1-10415481/How-can-I-terminate-a-thread-that-has-a-seperate-message ...

  4. 深入Delphi -- Windows 消息机制

    http://www.txsz.net/xs/delphi/3/Windows%20%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6.htm Windows 消息机制 by m ...

  5. IAR EWARM Example Download List

    https://srv.iar.com/ExamplesOnDemand/versions.xml http://netstorage.iar.com/SuppDB/Public/EXAMPLES/0 ...

  6. Qtwebkit flashplayer插件问题

      复制npswf32.dll 到 C:\WINDOWS\system32\Macromed\Flash\ 代码加入: //! [1] QNetworkProxyFactory::setUseSyst ...

  7. 不要滥用div,保持代码的整洁

    这篇文章算是很基础的了.旨在介绍如何保证页面代码的整洁.以维护性.使用有语义的页面标签,减少标签的滥用. 1. 移除不必要的<div>标签 嵌套在<form><ul> ...

  8. JDBC-ODBC桥接方法连接Excel数据库的方法

    通过JDBC-ODBC桥接器访问Excel电子表格 1.设置数据源 Excel数据源选择的驱动程序是Microsoft Excel Driver 2.选择表 与访问其他数据库不同的是,我们必须在电子表 ...

  9. maven中解决javax.servlet.jsp.PageContext cannot be resolved to a type

    在eclipse环境下用maven出现:javax.servlet.jsp.PageContext cannot be resolved to a type. 这是由于没有引入jsp-api引发的问题 ...

  10. Codeforces Beta Round #5 E. Bindian Signalizing 并查集

    E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...