Wednesday, April 11, 2007

C++, Java Native Interface (JNI)

In my recent Java Native Interface (JNI) adventures, I have discovered that when calling the JNI function NewObject in C++, you need to cast the variables to their corresponding jni type. I didn't have to do this when using C and jni.
Example:
C++ struct ABC { double x, double y };

Using C++ create a jni object to pass to java:

jobject createABC( JNIEnv* env, jobject obj )
{
ABC abc;
a.x = 5.2;
a.y = 2.4;

jclass cls = env->FindClass( "ABC" ); // find the java ABC class

jmethodID mid = env->GetMethodID( cls, "", "(DD)V" );

jobject jabc = env->NewObject( cls, mid, (jdouble)a.x, (jdouble)a.y);

return jabc;
}



Error Doctor 2007 ***Brand New 2007 Version! <br />

No comments: