简体   繁体   English

使用 long 将指针从 JNI 传递到 Java

[英]Passing a pointer from JNI to Java using a long

I'm trying to pass a structure as a pointer from JNI to Java to be able to pass it back later from Java to JNI.我正在尝试将结构作为指针从 JNI 传递给 Java,以便稍后能够将其从 Java 传递回 JNI。 I have read this thread: Passing pointers between C and Java through JNI , but I did not succeed.我已阅读此线程: 通过 JNI 在 C 和 Java 之间传递指针,但我没有成功。

I have a pretty complex structure: struct myStruct_s myStruct;我有一个非常复杂的结构: struct myStruct_s myStruct;

From Java, I call a JNI function to initialize the structure and to return a long (pointer to the structure):从 Java 开始,我调用 JNI function 来初始化结构并返回一个 long(指向结构的指针):

JNIEXPORT jlong JNICALL Java_example_ExampleJNI_getStruct(JNIEnv *jenv, jclass jcls) {
    struct myStruct_s mystruct;
    long *lp = (long*)&myStruct;
    return lp;
}

Then I call a Java method with that long in argument.然后我用那个长参数调用 Java 方法。 In JNI I want to be able to use the strcuture created earlier.在 JNI 中,我希望能够使用之前创建的结构。 I do like this:我喜欢这样:

JNIEEXPORT jint JNICALL Java_example_ExampleJNI_methode1(JNIEnv *jenv, jclass jcls, jlong jarg) {
    struct myStruct_s *arg = (struct myStruct_s *)&jarg;
    ...
}

Well it doesn't work.好吧,它不起作用。 I guess my cast of the long into the struct is wrong.我猜我对结构的长期转换是错误的。 How should I do it?我该怎么做? Thank you.谢谢你。


EDIT : Thanks for the hints, here are the working functions编辑:感谢您的提示,这里是工作功能

JNIEXPORT jint JNICALL Java_example_ExampleJNI_methode1(JNIEnv *jenv, jclass jcls, jlong jarg) {
    struct myStruct_s *arg;
    arg = (struct myStruct_s *)jarg;
    ...
} 

JNIEXPORT jlong JNICALL Java_example_ExampleJNI_getStruct(JNIEnv *jenv, jclass jcls) {
    struct myStruct_s *myStruct;
    myStruct = (struct myStruct_s *)malloc(sizeof(struct myStruct_s));
    long lp = (long)myStruct;
    return lp;
}

In your example在你的例子中

struct myStruct_s mystruct;

is a local variable on the stack, and therefore not available after the function returns.是堆栈上的局部变量,因此在 function 返回后不可用。 Possubly that's just a cut-down of your code, but if not then use a malloc(sizeof(struct myStruct_s)) to get yourself a heap allocation.可能这只是您的代码的缩减,但如果没有,则使用 malloc(sizeof(struct myStruct_s)) 为自己获得堆分配。

That then raises the question of when you are going to free that allocation, watch out for memory leaks.这就提出了何时释放该分配的问题,注意 memory 泄漏。

The memory of this structure is destroyed after the method is returned because it was put into the stack, not into the heap, try it:这个结构的memory因为是入栈而不是入堆,在方法返回后销毁,试试看:

JNIEXPORT jlong JNICALL Java_example_ExampleJNI_getStruct(JNIEnv *jenv, jclass jcls) {
    long *lp = (long*)malloc(sizeof(struct myStruct_s));
    return lp;
}

Ps: why long * and not simple long ? Ps:为什么long * 而不是简单的 long

In additiona to @Moise's suggestion I would cast the pointer to a long ratehr than a long *除了@Moise 的建议,我会将指针指向一个long ratehr 而不是long *

long lp = (long)&myStruct;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM