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();
}
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.
Comments