Re: Calling Java from C and back?
On Fri, 24 Jul 2009, Elena wrote:
I've developed a native application which calls Java by means of JNI and
everything is going fine. What I'd like to do now is collecting debug
logs from the Java side together with logs from the main native
application. That is, I have to call C from Java.
It doesn't seem that Java allows simple callbacks from Java to C.
Right. Java can only call specific named native functions, not arbitrary
ones identified only by a function pointer.
The lame solution I'm going to employ if I fail in finding a simpler
solution is to build a JNI DLL which stores C callbacks in some global
variable and then gets loaded from Java, therefore being able to call
those C callbacks.
Pass the function pointers to the callbacks to java as integers (or
longs). Create a native method which is a callback caller: java can call
it, passing in a function pointer and some parameters, and it will call
them. Does that make sense?
This is similar to what you're suggesting, i think, but it moves the data
storage to the java side, where it should be easier.
Formal logical proofs, and therefore programs - formal logical proofs
that particular computations are possible, expressed in a formal system
called a programming language - are utterly meaningless. To write a
computer program you have to come to terms with this, to accept that
whatever you might want the program to mean, the machine will blindly
follow its meaningless rules and come to some meaningless conclusion. --
Dehnadi and Bornat