Integration with C

Andrew Haley aph@redhat.com
Thu Apr 17 20:42:00 GMT 2003


Geert Bevin writes:
 > 
 > Andrew Haley wrote:
 > > Geert Bevin writes:
 > > > Andrew Haley wrote:
 > > > > No, you'll have to write a C interface in C++. It's still 10-100
 > > > > times faster than JNI.
 > > > 
 > > > Ok. Thanks a lot for this information. Another related question, can I 
 > > > work with function pointers to execute registered C callback hooks or is 
 > > > this not possible in the CNI architecture? Otherwise, I'll have to poll 
 > > > at regular intervals from within the gui, which is a pretty ugly 
 > > > solution ;-)
 > > 
 > > Yes, function pointers are fine for calling CNI hooks. That was one
 > > of the design goals. However, if you want to call a Java method
 > > you'll have to get the address of the object somehow.
 > 
 > I've read over the gcj documentation in try to find a logical way to 
 > register a collection of function pointers. The only structure that I 
 > can find is the gnu.gcj.RawData class which can contain anything but 
 > need to be cast to the correct type explicitely. Is this a way to do 
 > what I need, or am I overlooking something?
Well, it depends on what exactly you mean. I suppose that you want to
call Java from C, and in turn you want Java to be able to call back
into C.
Define a Java Class thusly:
class Callback
{
 RawData method;
 public static native int foo (int bar, int baz);
}
and the C++ thusly:
typedef int (*fptr)(int, int):
jint foo (jint bar, jint baz)
{
 fptr f = (fptr)method;
 (*f)(bar, baz);
}
But why bother? It might be easier just to keep a cache of function
pointers somewhere and pass around indexes instead.
 > It would be great to see some examples of this, do you know if
 > there are some available?
Well, you'll have to say what you want to do. 
Java certainly lets you have arrays of methods that you invoke; if
each one of these is a native method which calls a C method in turn
that'll surely work.
Andrew.


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /