使用长从JNI指针传递到Java [英] Passing a pointer from JNI to Java using a long
问题描述
我想传递一个结构,从JNI到Java的指针,以便能够从Java后传回JNI。我已阅读本主题:<一href=\"http://stackoverflow.com/questions/1632367/passing-pointers-between-c-and-java-through-jni\">Passing通过JNI C和Java之间的指针,但我没有成功。
我有一个pretty复杂的结构:结构myStruct_s MYSTRUCT;
从Java,我称之为JNI函数初始化结构和长(指针结构)返回:
JNIEXPORT jlong JNICALL Java_example_ExampleJNI_getStruct(JNIEnv的* jenv,JCLASS JCLS){
结构myStruct_s MYSTRUCT;
长* LP =(长*)及MYSTRUCT;
返回LP;
}
然后我说法调用Java方法,那么久。在JNI我希望能够使用之前创建的晶格结构。我这样做:
JNIEEXPORT jint JNICALL Java_example_ExampleJNI_methode1(JNIEnv的* jenv,JCLASS JCLS,jlong jarg){
结构myStruct_s * ARG =(结构myStruct_s *)及jarg;
...
}
那么这是行不通的。我想我长的投入结构是错误的。我应该怎么办呢?
谢谢你。
修改:谢谢你的提示,这里的工作职能
JNIEXPORT jint JNICALL Java_example_ExampleJNI_methode1(JNIEnv的* jenv,JCLASS JCLS,jlong jarg){
结构myStruct_s * ARG;
ARG =(结构myStruct_s *)jarg;
...
}JNIEXPORT jlong JNICALL Java_example_ExampleJNI_getStruct(JNIEnv的* jenv,JCLASS JCLS){
结构myStruct_s * MYSTRUCT;
MYSTRUCT =(结构myStruct_s *)malloc的(的sizeof(结构myStruct_s));
长LP =(长)MYSTRUCT;
返回LP;
}
在您的例子
结构myStruct_s MYSTRUCT;
在堆叠中的局部变量,因此在函数返回之后不可用。 Possubly这只是你的code的切割下来,但如果没有则使用一个malloc(sizeof的(结构myStruct_s)),以让自己的堆分配。
这则引发时,你要释放的分配,注意内存泄漏的问题。
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. I have read this thread: Passing pointers between C and Java through JNI, but I did not succeed.
I have a pretty complex structure : struct myStruct_s myStruct;
From Java, I call a JNI function to initialize the structure and to return a long (pointer to the structure):
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. In JNI I want to be able to use the strcuture created earlier. 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. 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.
That then raises the question of when you are going to free that allocation, watch out for memory leaks.
这篇关于使用长从JNI指针传递到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!