Integration with C

Geert Bevin
Fri Apr 18 14:09:00 GMT 2003

Hi Andrew,

thanks for the answers.

> 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.

What I want is a backend java library which can perform long operations 
in a background thread. A C/C++ GUI can link against this lib and 
register a number of callback hooks (as function pointers). Then a long 
operation is started in a seperate thread and the java library goes over 
the registered hooks (listeners) to report progress, messages, errors 
and finish. When the process is finished, the GUI is able to unregister 
the hooks too.

This would make is possible to write a reponsive GUI, that remains 
usable by a user while a long operation is being executed in the background.

> 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);
> }

I see how this would work, but my main question is how do you get the 
function point in the method variable. Here you only use it, you never 
provide it. Since the interface needs to be declared in java as being 
native, there's no way to write a method that takes a function pointer 
argument, is there? Would it be possible to derive the Callback class in 
C++ alone and add a setCallback(fptr callback) method? It would even be 
better if this would be possible in the Callback class inself, but I 
suppose that's not an option.

> But why bother?  It might be easier just to keep a cache of function
> pointers somewhere and pass around indexes instead.

So you'd store a list of function pointers in a C++ class and work with 
integer position identifiers instead. That feels a bit 'dirty' to me ;-) 
I like to work was as much typed information as possible.

>  > 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.  

See above :-)

> 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.

Take care,


Geert Bevin                       Uwyn
"Use what you need"               Lambermontlaan 148               1030 Brussels
gbevin[remove] at uwyn dot com    Tel & Fax +32 2 245 41 06

PGP Fingerprint : 4E21 6399 CD9E A384 6619  719A C8F4 D40D 309F D6A9
Public PGP key  : available at servers,

More information about the Java mailing list