I've been using Java and C for a while but I'll need a method to pass whole
classes to C++ (and backwards).
The Tutorials about JNI don't show a way to do this. Anyone out there, who
knows a method?
Even (easy to handle, so I can use it with several classes) workarounds
would do.
If there's no way to with classes perhaps structs would do.
Thanks,
Stefan
> Hi NG.
>
> I've been using Java and C for a while but I'll need a method to pass
> whole classes to C++ (and backwards).
What do you mean "pass whole classes"? What would C++ do with this object?
Do you mean classes as in code, or classes as in data? Or both?
--
Paul Lutus
www.arachnoid.com
> > Hi NG.
> >
> > I've been using Java and C for a while but I'll need a method to pass
> > whole classes to C++ (and backwards).
>
> What do you mean "pass whole classes"? What would C++ do with this object?
> Do you mean classes as in code, or classes as in data? Or both?
The Idea is to "recycle" C++ Code with an Java(Swing) GUI.
So I have to transmit several objects containing Data.
e.g. There's C++ Code getting data from a Cam in realtime.
After that, some complex mathematical calculations are made (realtime!).
I would like to pass information about that to my swing-GUI. If somthing
went wrong I'd like to display the taken picture too.
Oh! The more I think about it the more I feel there's no need to pass the
code.
Ok then: structs would do.
I'm looking for something like:
(C++): myPictureClass /*or struct */ returnPictureToJava();
this function might use the JNI-Callback. But: how can I
submit some
more complex Information "en block"?
Thanks again :)
Stefan
<snip>
>
> I'm looking for something like:
> (C++): myPictureClass /*or struct */ returnPictureToJava();
> this function might use the JNI-Callback. But: how can I
> submit some
> more complex Information "en block"?
I'll cut and paste a reply of mine from a similar question a while back:
===========================
Say you have a simple C++ class like this:
#include "CPPObject.h"
#include <stdio.h>
CppObject::CppObject(int aFoo) {
foo = aFoo;
}
void CppObject::printFoo() {
printf ("Value of foo is: %i\n", foo);
}
You can create a peer class in Java that has the same public interface
as the C++ class, but delegates the construction and method calls to
some JNI code:
public class CppObjectPeer {
static {
System.loadLibrary("cppobjects");
}
protected long ptr;
public CppObjectPeer(int aFoo) {
this.ptr = createCppObject(aFoo); //create object in native code
}
public void printFoo() {
printFoo(this.ptr); //print from native code
}
//native methods
private final native long createCppObject(int aFoo);
private native void printFoo(long ptr);
}
Notice that when you need to create a C++ object, you call the Java
constructor which then calls into JNI code. The native call returns a
'long' value, which is stored in the 'ptr' variable.
The native method createCppObject() looks like this:
JNIEXPORT jlong JNICALL Java_CppObjectPeer_createCppObject
(JNIEnv *env, jobject obj, jint fooValue) {
CppObject *cppObj = new CppObject(fooValue);
return reinterpret_cast<jlong>(cppObj);
}
It simply creates the object, casts it to a jlong which is returned.
Similarly, when you need to call a method of the C++ object, you call
the corresponding Java method, which then calls into the JNI code,
passing in the pointer:
JNIEXPORT void JNICALL Java_CppObjectPeer_printFoo
(JNIEnv *env, jobject obj, jlong ptr) {
CppObject *cppObj = reinterpret_cast<CppObject*>(ptr);
cppObj->printFoo();
}
So it is simply a matter of writing a little bit of JNI 'glue' code to
pass pointers back and forth. If you need to call methods that have
more complex arguments or return values, you have to add some code to
translate those items each time you cross the JNI interface as well.
Note that the above code is a trivial example, but it shows the general
idea.
========================
Jim S.