2

I have a question related to pointers to pointers in JNI.

If I have the following function in C:

void myfunction(int **pp);

How do I get the reference of int **pp in Java? And how do I pass to a JNI function the reference in order to modify it?

Can you lead me to a solution? I can work with simple pointers but not with pointers to pointers.

4

2 回答 2

4

等效的 JNI 会用一个长原始数组代替方法的第一个参数。

int alloc_opaque_type(T** pp);

对比

public static native int alloc_opaque_type(long[] pp);
于 2013-06-04T13:50:22.217 回答
1

在 Java 中,您不能将 Java 对象作为指针来修改它,如果这是您所要求的。也不能将 Java 对象直接转换为 C 结构或 C++ 对象。但是,您可以将 Java 对象获取为jobject,手动获取其原始值(例如字符串中的整数或字符)并使用这些值在 C 代码中构造您需要的任何内容。您可以投射jintintjdoubledouble等...,但不能投射jobjectstruct或数组。

如果您获得了一个指针并将其传递给 Java(作为int),那么您可以将其传递int回 C(通过 JNI)并将其转换int回指针。

于 2013-06-07T22:26:49.087 回答