如何使用JNI来传递从C code结构到Java [英] how to pass structure from c code to java using Jni

查看:162
本文介绍了如何使用JNI来传递从C code结构到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要传递一个结构

   struct Info {
    u_int8_t timestamp[8];
    u_int32_t  a;
    u_int32_t  b;
    u_int32_t  c;
    ActiveInfo activeInfo[MAX_ACTIVE_SET];

   };

 struct ActiveInfo
{
   u_int8_t  is_reference;
   u_int16_t p;
   u_int32_t q;
    u_int8_t  r;
   u_int8_t  s;
  };
  typedef struct ActiveInfo ActiveInfo;

我想这(信息)结构传递给我的Java code.I都瞪大眼睛,但不能获得完整的方法来做到这一点。

I want to pass this (Info) structure to my java code.I have goggled,but not get complete ways to do this.

感谢。

推荐答案

结构必须在Java方面来定义,与成员的类。的事实上,JNI允许C访问Java对象,而不是Java访问C对象(结构)的。所以,如果你想通,通过JNI的东西,有它的两面访问,它必须是一个Java对象,然后根据 jobject 合格的接口。从C面的话,你有两个选择:

The structure must be defined at Java side, as a class with members. The fact is that JNI allows C to access Java objects, but not Java to access C objects (structs). So if you want to "pass" something through JNI and have it accessible on both sides, it must be a Java object, then qualified as jobject in the interface. From C side then, you have two options:


  • 无论是与 GetFieldID()直接访问成员 get / set方法<类型>现场,但它更使用数组复杂(你有一些我看到)

  • 或创建该类的Java方法填补,并在简化的方式阅读,并与调用&LT调用它们; RETVAL>方法

  • either access the members directly with GetFieldID() and Get/Set<Type>Field, though it's more complicated with arrays (you got some i see)
  • or create Java methods in that class for filling up and reading in simplified fashion, and invoke them with Invoke<Retval>Method

这要看你的数据存储设计。你或许希望仅在一侧(C或Java)来读,另一个写,这可以在设计方便地反映出来。

It depends on design of your data storage. You perhaps want only one side (C or Java) to read and the other to write, which can be reflected in the design conveniently.

编辑:

实例可以在网站上找到所指出@asgoth: www.steveolyo.com 。有一个名为从C传递C结构到Java一章,但随后静静地解释如何反映Java类requried的C结构,并通过Java对象转换为C通过JNI - 这正是我的回应说什么

Example can be found at site pointed out by @asgoth : www.steveolyo.com. There is a chapter named "Passing C Structures from C to Java" but then it silently explains how to reflect requried C structure in Java class and pass Java object into C via JNI - which is exactly what my response says.

这篇关于如何使用JNI来传递从C code结构到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆