I have a function already implemented in cpp with prototype
MyFunction (int size, int (* callback) (UINT16 * arg1, UINT16 * arg2));
The second argument is a pointer to a function to be implemented in java. How can I implement this feature? Also how can I call MyFunction in JNI? Please, help
try it
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 will be called from inside the native method public String Callback(String msg){ return "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 from Setcallback: %s", nativeresult); jnienv->ReleaseStringUTFChars(result, nativeresult); }
The idea here is to call a method in Java through an instance of the class. If you do not know the name of the java function in advance, the name of the function can also be passed as a parameter.
: http://www.tidytutorials.com/2009/07/java-native-interface-jni-example-using.html
++ Java, - :
native c++: // link func callback_link(int size, int (* callback)(UINT16* arg1, UINT16* arg2)){ //jni->call Java implementation } // call your lib MyFunction(int size, int (* callback_link)(UINT16* arg1, UINT16* arg2));
Source: https://habr.com/ru/post/1537525/More articles:Backpack with weight and item limit - algorithm12/24 hour conflict mode - androidHow to implement SlopeOne recommendation in Mahout 0.9? - mahoutΠΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΡΠ»Π΅Π½Π° Π±Π°Π·ΠΎΠ²ΠΎΠ³ΠΎ ΠΊΠ»Π°ΡΡΠ° Π² ΠΊΠ»Π°ΡΡΠ΅, Π²Π»ΠΎΠΆΠ΅Π½Π½ΠΎΠΌ Π² ΡΠ°Π±Π»ΠΎΠ½ Π² Π‘++ - c++Selenium - get all iframes on a page (even nested)? - pythonGCE project with pending removal - google-compute-engineΠΠ°ΠΉΡΠΈ ΡΡΠ°ΡΠΈΡΡΠΈΠΊΡ Π·Π°ΠΊΠ°Π·Π° Π² ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠΈ 2 ΠΎΡΡΠΎΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ ΡΠΏΠΈΡΠΊΠΎΠ² Π² Π»ΠΎΠ³Π°ΡΠΈΡΠΌΠΈΡΠ΅ΡΠΊΠΎΠΌ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ - algorithmHow to block the main thread until one or more fibers end? - node.jsHow to produce visualized output from Sling POST to AEM? - aemHow can I cleanly override the standard ServiceLocator used by Jersey? - javaAll Articles