I need to implement callback function in Java using “interface”. I have wrote the application part as MyJavaFunction(int size, m_GetSizeInterface);
m_GetSizeInterface is an Interface which contains the callback function GetSize. This GetSize method is override in the application. In JNI I need to call a CPP function having prototype int MyCPPFunction(int size, int (*callback)(int* ID));
How can I pass this GetSize as parameter to MyCPPFunction in JNI? Please help
public int GetSize (m_SizeClass arg0)
{
g_size = arg0.size;
return 0;
}
The complication here is that you want to invoke native C++ code which you, in turn, want to invoke a java method. This is actually a bit tricky.
You need to create a JNI C++ function for java to call, and a C++ function matching the MyCPPFunction callback signature. The latter will act as a wrapper to call the java method.
Because the wrapper will need information about the JNI environment, which cannot be provided by parameters (lest we ruin the signature) you create a few global variables to hold it:
The C++ function which java will call is the following:
And the wrapper function is thus:
//and test code is: