0

I have a simple cpp file with extern functions. One function returns the reference of class Abc and other one receives it and calls some method:

extern "C" {

void* getMyObject(int arg1, int arg2)
     return new Abc();
}

void testMyObject(Abc* p) {
     p->test()
}

And a Java code which invokes the extern functions:

@Platform(include="export.cpp", link="mylib")
class MyIterface {
  static { 
     Loader.load()
  }

public static native Pointer getMyObject(int a, int b);
public static native void testMyObject(Pointer op);
}

I call them as follows:

MyInterface lib = new MyInterface();
Pointer op = lib.getMyObject(111, 222); //get the reference to Abc object
lib.testMyObject(op); //Pass the Abc pointer to invoke its method

But I get the following error:"

/home/Project/target/classes/com/proj/dl4j/jniMyInterface.cpp: In function ‘void Java_com_proj_dl4j_MyInterface_testMyPointer(JNIEnv*, jclass, jobject)’:
/home/Project/target/classes/com/proj/dl4j/jniMyInterface.cpp:629:27: error: cannot convert ‘char*’ to ‘Abc*’ for argument ‘1’ to ‘void testMyPointer(Abc*)’
         testMyPointer(ptr0);
                           ^
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.464 s
[INFO] Finished at: 2020-06-22T12:22:47+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.bytedeco:javacpp:1.5.3:build (process-classes) on project MyProject: Execution process-classes of goal org.bytedeco:javacpp:1.5.3:build failed: Process exited with an error: 1 -> [Help 1]

I did casting on the void* that is: Abc* ob = (Abc*)p which works. But I am restricted to modify these extern functions (Design issues !!). Is there any workaround ??

1 Answers1

1

Modifying the method to public static native void testMyObject(@Cast("Abc *") Pointer op); worked !