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 Java

passing pointer to C++ from python using pybind11

From Dev

Return a class pointer to Java by JNI

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 Dev

passing pointer to function and using realloc

From Dev

Passing from an incompatible pointer type

From Dev

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

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

Using GetDirectBufferAddress from JNI

From Dev

Opencv passing Mat from Android to JNI error

From Dev

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

From Java

didDiscoverCharacteristics not getting called when using CoreBluetooth from Java via JNI

From Java

Call static Java method from separate thread using JNI

From Dev

How to call a function with arguments in C++ from JAVA using JNI?

From Java

Copying Data from Java to C++ Objects Array Using JNI

From Java

Using a java socket from JNI / C++ code

From Dev

Keeping a native object in memory between calls from Java using JNI

From Dev

Return list<unsigned char*> from C++ to Java 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

    passing pointer to C++ from python using pybind11

  11. 11

    Return a class pointer to Java by JNI

  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

    passing pointer to function and using realloc

  15. 15

    Passing from an incompatible pointer type

  16. 16

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

  17. 17

    Using Scala from Java: passing functions as parameters

  18. 18

    Parameter Passing using java/jdbc from a JtextField

  19. 19

    Passing parameters from Java to Python using Jython

  20. 20

    Using GetDirectBufferAddress from JNI

  21. 21

    Opencv passing Mat from Android to JNI error

  22. 22

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

  23. 23

    didDiscoverCharacteristics not getting called when using CoreBluetooth from Java via JNI

  24. 24

    Call static Java method from separate thread using JNI

  25. 25

    How to call a function with arguments in C++ from JAVA using JNI?

  26. 26

    Copying Data from Java to C++ Objects Array Using JNI

  27. 27

    Using a java socket from JNI / C++ code

  28. 28

    Keeping a native object in memory between calls from Java using JNI

  29. 29

    Return list<unsigned char*> from C++ to Java using JNI

HotTag

Archive