AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

An example of a Java application calling C, and the C callin

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Clayton E. Cramer
Guest





PostPosted: Mon Jul 26, 2004 5:11 am    Post subject: An example of a Java application calling C, and the C callin Reply with quote



I have been able to use the JNI to write a Java application that calls C
functions, but I am having a heck of a time getting the C functions to
reliably call Java methods. I believe that the problem has to do with
multiple threads and not having the correct value for the JNIEnv when the C
function decides to invoke a Java method. There must be some way for a C
function to retrieve the current JNIEnv that invoked this C function,
without passing it in.

Please?


Back to top
Roedy Green
Guest





PostPosted: Mon Jul 26, 2004 8:17 am    Post subject: Re: An example of a Java application calling C, and the C ca Reply with quote



On Sun, 25 Jul 2004 23:11:11 -0600, "Clayton E. Cramer"
<clayton (AT) claytonDELETETHIScramer (DOT) com> wrote or quoted :

Quote:
I have been able to use the JNI to write a Java application that calls C
functions, but I am having a heck of a time getting the C functions to
reliably call Java methods. I believe that the problem has to do with
multiple threads and not having the correct value for the JNIEnv when the C
function decides to invoke a Java method. There must be some way for a C
function to retrieve the current JNIEnv that invoked this C function,
without passing it in.

Methods you may find of interest
JNICreateJavaJVM -- fires up a brand new JVM inside a standalone C
Program.

which will give you an env.

You can start up a new thread in the JVM with AttachCurrenThread.

I suggest you look at http://mindprod.com/jgloss/jni.html
and follow the link to Liang's book, chapter 7.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Back to top
Jim Sculley
Guest





PostPosted: Mon Jul 26, 2004 9:23 pm    Post subject: Re: An example of a Java application calling C, and the C ca Reply with quote



Clayton E. Cramer wrote:
Quote:
I have been able to use the JNI to write a Java application that calls C
functions, but I am having a heck of a time getting the C functions to
reliably call Java methods. I believe that the problem has to do with
multiple threads and not having the correct value for the JNIEnv when the C
function decides to invoke a Java method. There must be some way for a C
function to retrieve the current JNIEnv that invoked this C function,
without passing it in.

Er, every JNI invocation of a C function should already have the JNIEnv
pointer (for the current thread) passed in. Are you creating threads on
the native side? Then you might have a problem. If so, see the
AttachCurrentThread function:

http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/invocation.html#wp1060
====================
The JNI interface pointer (JNIEnv) is valid only in the current thread.
Should another thread need to access the Java VM, it must first call
AttachCurrentThread() to attach itself to the VM and obtain a JNI
interface pointer. Once attached to the VM, a native thread works just
like an ordinary Java thread running inside a native method. The native
thread remains attached to the VM until it calls DetachCurrentThread()
to detach itself.
====================

Jim S.


--
Remove my extraneous mandibular appendages to reply via email.

Back to top
Clayton E. Cramer
Guest





PostPosted: Mon Jul 26, 2004 11:38 pm    Post subject: Re: An example of a Java application calling C, and the C ca Reply with quote


"Roedy Green" <look-on (AT) mindprod (DOT) com.invalid> wrote

Quote:
On Sun, 25 Jul 2004 23:11:11 -0600, "Clayton E. Cramer"
[email]clayton (AT) claytonDELETETHIScramer (DOT) com[/email]> wrote or quoted :

I have been able to use the JNI to write a Java application that calls C
functions, but I am having a heck of a time getting the C functions to
reliably call Java methods. I believe that the problem has to do with
multiple threads and not having the correct value for the JNIEnv when the
C
function decides to invoke a Java method. There must be some way for a C
function to retrieve the current JNIEnv that invoked this C function,
without passing it in.

Methods you may find of interest
JNICreateJavaJVM -- fires up a brand new JVM inside a standalone C
Program.

which will give you an env.

This isn't a standalone C program. Java application calls C functions; the
C functions,
much later and deeper, need to call Java methods. I could change all the C
code to
pass the JNIEnv down to the C functions that do a callback to the Java
methods, but I was
hoping for some other way to do this. Apparently caching the JNIEnv from
the first Java
invocation of the C function doesn't work reliably for later invocations of
the C functions.

Quote:
You can start up a new thread in the JVM with AttachCurrenThread.

I suggest you look at http://mindprod.com/jgloss/jni.html
and follow the link to Liang's book, chapter 7.

Thanks for the pointer.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.