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.
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 />“id=”BLOGGER_PHOTO_ID_5052354926391138034” /></a></p>

			</div><!-- .entry-content -->
		<footer class= Leave a Comment on C++, Java Native Interface (JNI)/ Edit

Use C++ to pass a function a base class and return a derived class, user defined variables

Sorry I haven’t had any new apache batik/svg updates.
I’m into some C++ work now and haven’t had time to work on it.
Pointers, I hate them, definitely a pain in the butt.
I searched all day on the internet and couldn’t find any examples showing me how to do what I wanted. I finally took a look at some old third party code I have & figured out what I needed.
So here’s a rundown of what I was trying to do and what I figured out.
I have a base class A with an int x.
I have a bunch of derived classes of A, each with their own set of variables.
Let’s work with derived class B, which has a double i.
I have a function which accepts an A and sets an array of user defined variables for each derived class.
The array type is a struct, part of which is a union made up of pointers to each derived class.
class A
int x;

class B : public A
double i;

union P
B* b;

struct Row
int status;
P p;

Row values[2];

void setInfo( int index, A* _a )
// just for this example, i know _a is a B
values[index].p.b = static_cast(B*)(_a);

I call setInfo like this:
B* b1 = new B( 4, 5.2 ); // 4 gets passed to base class A, so x = 4, i = 5.2
BTW setInfo lives in another class c, so
c->setInfo( 0, b1 );

Ok. Now I want a get function to return B, but I need it returned in the parameter list, not the easy way by using the function return value. The reason for this is in my real work, I have multiple values which need to be returned.
So here’s what my get function looks like:

getInfo( int index, A& _a )
// just for this example, i know _a is a B
(static_cast(A&)(_a)) = (*values[index].p.b);

I call getInfo like this:
B b2;
c->getInfo( 0, b2 );


So this is how I pass a function multiple base class parameters and return multiple derived class parameters containing user defined variables. Easy enough I guess. I still hate pointers though.

Error Doctor 2007 ***Brand New 2007 Version