java - Call function pointer from JNI -
java - Call function pointer from JNI -
i have function implemented in cpp prototype
myfunction(int size, int (* callback)(uint16* arg1, uint16* arg2));
second argument function pointer must implemented in java. how can implement function? how can phone call myfunction in jni? please help
try this
java
import java.util.*; public class jniexample{ static{ try{ system.loadlibrary("jnicpplib"); }catch(exception e){ system.out.println(e.tostring()); } } public native void setcallback(jniexample jniexample); public static void main(string args[]){ (new jniexample()).go(); } public void go(){ setcallback(this); } //this called within native method public string callback(string msg){ homecoming "java callback echo:" +msg; } }
in c++ native:
#include "jniexample.h" jniexport void jnicall java_jniexample_setcallback (jnienv * jnienv, jobject jobj, jobject classref) { jclass jc = jnienv->getobjectclass(classref); jmethodid mid = jnienv->getmethodid(jc, "callback","(ljava/lang/string;)ljava/lang/string;"); jstring result = (jstring)jnienv->callobjectmethod(classref, mid, jnienv->newstringutf("hello jni")); const char * nativeresult = jnienv->getstringutfchars(result, 0); printf("echo setcallback: %s", nativeresult); jnienv->releasestringutfchars(result, nativeresult); }
the thought here calling method in java through class instance. in case not know name of java function in advance function name can passed parameter well.
for more information: http://www.tidytutorials.com/2009/07/java-native-interface-jni-example-using.html
java jni function-pointers
Comments
Post a Comment