如何通过C结构来回的Java code在JNI? [英] How to pass C structs back and forth to Java code in JNI?

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

问题描述

我已经得到了它,我通过JNI调用一些C函数其中一个指针指向的结构,以及其他一些功能,这将分配/释放一个指向相同类型的结构,使其更容易一点处理我的包装。出人意料的是,JNI文件说,甚少如何处理C结构。

I've got some C functions which I am calling through JNI which take a pointer to a structure, and some other functions which will allocate/free a pointer to the same type of structure so that it is a bit easier to deal with my wrapper. Surprisingly, the JNI documentation says very little about how to deal with C structures.

我的C头文件看起来像这样:

My C header file looks like so:

typedef struct _MyStruct {
  float member;
} MyStruct;

MyStruct* createNewMyStruct();
void processData(int *data, int numObjects, MyStruct *arguments);

相对应的JNI C包装文件包含:

The corresponding JNI C wrapper file contains:

JNIEXPORT jobject JNICALL
Java_com_myorg_MyJavaClass_createNewMyStruct(JNIEnv *env, jobject this) {
  return createNewMyStruct();
}

JNIEXPORT void JNICALL
Java_com_myorg_MyJavaClass_processData(JNIEnv *env, jobject this, jintArray data,
                                       jint numObjects, jobject arguments) {
  int *actualData = (*env)->GetIntArrayElements(env, data, NULL);
  processData(actualData, numObjects, arguments);
  (*env)->ReleaseIntArrayElements(env, data, actualData, NULL);
}

...最后,相应的Java类:

...and finally, the corresponding Java class:

public class MyJavaClass {
  static { System.loadLibrary("MyJniLibrary"); }

  private native MyStruct createNewMyStruct();
  private native void processData(int[] data, int numObjects, MyStruct arguments);

  private class MyStruct {
    float member;
  }

  public void test() {
    MyStruct foo = createNewMyStruct();
    foo.member = 3.14159f;
    int[] testData = new int[10];
    processData(testData, 10, foo);
  }
}

不幸的是,code击中后立即崩溃的JVM createNewMyStruct()。我是一个有点新的JNI,而且不知道这个问题可能是。

Unfortunately, this code crashes the JVM right after hitting createNewMyStruct(). I'm a bit new to JNI and have no idea what the problem could be.

修改:我应该注意到,C code是非常香草C,是良好的测试,并从工作iPhone项目被移植。此外,该项目采用了Android NDK框架,它可以让你从Android项目从内部JNI运行原生的C code。不过,我不认为这是严格意义上的NDK问题......好像我的一个JNI设置/初始化错误。

Edit: I should note that the C code is very vanilla C, is well-tested and was ported from a working iPhone project. Also, this project is using the Android NDK framework, which lets you run native C code from an Android project from within JNI. However, I don't think that this is strictly an NDK issue... it seems like a JNI setup/initialization error on my part.

推荐答案

您需要创建具有相同成员的C结构的Java类,并通过方法在C code'地图'他们env-> GetIntField ,env-> SetIntField,env-> GetFloatField,env-> SetFloatField,等等 - 总之,大量的手工劳动,希望有已经存在,可以自动做计划:JNAerator(的 HTTP://$c$c.google.com/p/jnaerator )和SWIG(的 http://www.swig.org/ )。两者都有自己的利弊,选择权在你。

You need to create a Java class with the same members as C struct, and 'map' them in the C code via methods env->GetIntField, env->SetIntField, env->GetFloatField, env->SetFloatField, and so on - in short, lots of manual labor, hopefully there already exist programs that do it automatically: JNAerator (http://code.google.com/p/jnaerator) and SWIG (http://www.swig.org/). Both have their pros and cons, the choice is up to you.

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

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