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. 局部化原理(Laplace渐进估计方法)

    设$f(x)$于$[0,1]$上严格单调递减,且$f(0)=1,f(1)=0$,证明: $$\int_{0}^{1}f^{n}(x)dx \sim \int_{0}^{\delta}f^{n}(x), ...

  2. 分享一个导出Excel时页面不跳转的小技巧

    今天在点击客户档案导出的时候,发现先是打开了一个新标签,然后新标签自动关掉,弹出一个文件下载确认的窗口,点击确认后开始下载导出的Excel文件.这样的过程感觉窗口闪来闪去,而且可能会给用户带来困惑,是 ...

  3. arm_cm4.c关于kinetis的修改

    /***********************************************************************/ /* * Initialize the NVIC t ...

  4. ASP.NET 最佳DataGrid读取单元格的值

    采用asp:BoundColumn列 前台: <asp:BoundColumn DataField="UserID" HeaderText="工号"> ...

  5. ST10 Bootstrap Loader

    Bootstrap Loader The built-in bootstrap loader (BSL) of the ST10F269 provides a mechanism to load th ...

  6. sqldependency 支持的select

    https://msdn.microsoft.com/library/ms181122.aspx   支持的 SELECT 语句 满足下列要求的 SELECT 语句支持查询通知: 必须显式说明 SEL ...

  7. 从头学Android系列

    从头学Android系列 http://blog.csdn.net/worker90/article/category/888358

  8. 使用NuGet安装EntityFramework4.2

    1.下载NuGet 有两种方式下载NuGet 第一种:在微软的档案库下载,下载地址为:http://visualstudiogallery.msdn.microsoft.com/27077b70-9d ...

  9. Android Task 与 Back Stack

    Tasks and Back Stack 一个应用通常包括多个 activity.每个 activity应用设计为围绕针对执行用户特定的行为和可以启动其它 activity. 一个 Activity也 ...

  10. 无需图片,使用CSS3实现圆角按钮[转]

    首先来看看效果:   事例HTML代码: <a href="#" class="button green">button</a> < ...