Passing a pointer from JNI to Java using a long

leochab :

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;
}
djna :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Passing strings from java to c++ using JNI

From Dev

Passing float[] from C++ to Java using JNI

From Java

Passing pointer from java to native

From Java

passing string array from java to C with JNI

From Java

JNI: passing integer array from Java to C

From Dev

Efficiently passing GPB serialized data from Java to C++ using JNI

From Dev

Passing function pointer from native to java

From Dev

JNI passing a long value to a native method

From Dev

Call fortran dll from java using JNI

From Dev

Return a class pointer to Java by JNI

From Java

passing pointer to C++ from python using pybind11

From Java

JNA passing the equivalent of swift pointer from Java Android to C

From Dev

Why is passing java long as pointer to struct in c++ library crashing app?

From Java

Using GetDirectBufferAddress from JNI

From Dev

Processing a string into JNI layer by passing through java API and creating a buffer using malloc

From Dev

Opencv passing Mat from Android to JNI error

From Java

Passing pointers between C and Java through JNI

From Java

Passing double-byte (WCHAR) strings from C++ to Java via JNI

From Dev

Passing Integer arguments from Java function to JNI function returning garbage values?

From Dev

passing pointer to function and using realloc

From Dev

how to reuse objects between a java/rust service via JNI using raw pointer

From Dev

Passing pointer to pointer to float from Java through JNA to a C dynamic library

From Dev

Passing from an incompatible pointer type

From Java

Using Scala from Java: passing functions as parameters

From Dev

Parameter Passing using java/jdbc from a JtextField

From Dev

Passing parameters from Java to Python using Jython

From Java

Get the pointer of a Java ByteBuffer though JNI

From Java

didDiscoverCharacteristics not getting called when using CoreBluetooth from Java via JNI

From Java

Call static Java method from separate thread using JNI

Related Related

  1. 1

    Passing strings from java to c++ using JNI

  2. 2

    Passing float[] from C++ to Java using JNI

  3. 3

    Passing pointer from java to native

  4. 4

    passing string array from java to C with JNI

  5. 5

    JNI: passing integer array from Java to C

  6. 6

    Efficiently passing GPB serialized data from Java to C++ using JNI

  7. 7

    Passing function pointer from native to java

  8. 8

    JNI passing a long value to a native method

  9. 9

    Call fortran dll from java using JNI

  10. 10

    Return a class pointer to Java by JNI

  11. 11

    passing pointer to C++ from python using pybind11

  12. 12

    JNA passing the equivalent of swift pointer from Java Android to C

  13. 13

    Why is passing java long as pointer to struct in c++ library crashing app?

  14. 14

    Using GetDirectBufferAddress from JNI

  15. 15

    Processing a string into JNI layer by passing through java API and creating a buffer using malloc

  16. 16

    Opencv passing Mat from Android to JNI error

  17. 17

    Passing pointers between C and Java through JNI

  18. 18

    Passing double-byte (WCHAR) strings from C++ to Java via JNI

  19. 19

    Passing Integer arguments from Java function to JNI function returning garbage values?

  20. 20

    passing pointer to function and using realloc

  21. 21

    how to reuse objects between a java/rust service via JNI using raw pointer

  22. 22

    Passing pointer to pointer to float from Java through JNA to a C dynamic library

  23. 23

    Passing from an incompatible pointer type

  24. 24

    Using Scala from Java: passing functions as parameters

  25. 25

    Parameter Passing using java/jdbc from a JtextField

  26. 26

    Passing parameters from Java to Python using Jython

  27. 27

    Get the pointer of a Java ByteBuffer though JNI

  28. 28

    didDiscoverCharacteristics not getting called when using CoreBluetooth from Java via JNI

  29. 29

    Call static Java method from separate thread using JNI

HotTag

Archive