Return a class pointer to Java by JNI

Sky

There is a existing class pointer in C++ How do i get this class pointer and converter to class in java by JNI

Class_A_B.cpp

Class ClassB
{
public:
    int funcA();
    int funcB();
}
Class ClassA
{
public:
    virtual void func(){B = new ClassB();};
    ClassB* B;
    ClassB* GetClassB(){return B;};
}

Jni_Class_A_B.cpp

ClassA TestClassA = new ClassA();

extern "C"
JNIEXPORT jobject JNICALL Java_com_core_Android_getClassB(JNIEnv *env, jobject thiz)
{
    return (jobject)TestClassA->GetClassB();    // It will crash, how to converter class pointer to jobject?
}

GetClassB.java

public native ClassB getClassB();
public void TestFunction()
{
    ClassB B = getClassB();
}
Stephen C

There is no way to convert a pointer to a C++ native object (a "class pointer" as you put it) into a jobject. Indeed, if you could do this, you would most likely hard-crash the JVM ... 'cos the Java GC would not be able to cope with the C++ object.

The best you could do would be to pass / return the C++ pointer to Java as a 32 or 64 bit integer, and then use another JNI call if the Java code needs to access the C++ object's state or call a method on it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Use JNI to Create, Populate and Return a Java Class Instance

From Java

Get the pointer of a Java ByteBuffer though JNI

From Java

Passing a pointer from JNI to Java using a long

From Dev

java lang class exception -Unable to cast the java vector<user defined java class> return from c++ jni object callback

From Dev

Return a struct pointer within a class

From Dev

How to return string array to java JNI

From Java

How to return an array from JNI to Java?

From Java

How to return int array from Java to JNI

From Dev

How to call class method from JNI and cast the return to a custom class

From Java

Finding if a Java class is final in JNI by using reflection

From Dev

Access Java class object methos from JNI

From Java

Returning a C++ class to Java via JNI

From Java

How to instantiate shared pointer with JNI to call java implementation

From Java

Is there any way to get a direct pointer to a Java array via JNI?

From Dev

Error in `/usr/bin/java': munmap_chunk(): invalid pointer: in JNI

From Dev

how to properly convert byte array in java to char pointer in jni

From Dev

Return a function pointer from a class member function

From Dev

class name as return type & use of this pointer

From Dev

How to return nested class pointer from method?

From Dev

Return pointer to a vector from a Class C++

From Dev

Return derived object pointer in base class object

From Dev

Return Class function pointer c++

From Dev

How to pass a structure as an argument to java function or return to java from jni

From Java

Java: Return class (Not an instance)

From Dev

Return Base class shared pointer using derived class C++

From Java

JNI Android - Converting char* to byte array and return it to java

From Dev

What does a Java method return to a JNI caller when it throws an exception?

From Dev

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

From Dev

Return a string from JNI (C++) code to Java

Related Related

  1. 1

    Use JNI to Create, Populate and Return a Java Class Instance

  2. 2

    Get the pointer of a Java ByteBuffer though JNI

  3. 3

    Passing a pointer from JNI to Java using a long

  4. 4

    java lang class exception -Unable to cast the java vector<user defined java class> return from c++ jni object callback

  5. 5

    Return a struct pointer within a class

  6. 6

    How to return string array to java JNI

  7. 7

    How to return an array from JNI to Java?

  8. 8

    How to return int array from Java to JNI

  9. 9

    How to call class method from JNI and cast the return to a custom class

  10. 10

    Finding if a Java class is final in JNI by using reflection

  11. 11

    Access Java class object methos from JNI

  12. 12

    Returning a C++ class to Java via JNI

  13. 13

    How to instantiate shared pointer with JNI to call java implementation

  14. 14

    Is there any way to get a direct pointer to a Java array via JNI?

  15. 15

    Error in `/usr/bin/java': munmap_chunk(): invalid pointer: in JNI

  16. 16

    how to properly convert byte array in java to char pointer in jni

  17. 17

    Return a function pointer from a class member function

  18. 18

    class name as return type & use of this pointer

  19. 19

    How to return nested class pointer from method?

  20. 20

    Return pointer to a vector from a Class C++

  21. 21

    Return derived object pointer in base class object

  22. 22

    Return Class function pointer c++

  23. 23

    How to pass a structure as an argument to java function or return to java from jni

  24. 24

    Java: Return class (Not an instance)

  25. 25

    Return Base class shared pointer using derived class C++

  26. 26

    JNI Android - Converting char* to byte array and return it to java

  27. 27

    What does a Java method return to a JNI caller when it throws an exception?

  28. 28

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

  29. 29

    Return a string from JNI (C++) code to Java

HotTag

Archive