Hi Nathan,

Thank you for your response.  Please let me re-address my question by using
the following pseudo code.  I embedded my question in so2.c pseudo code.

The following lines are app.java, so1.c and so2.c.  They are only pseudo
code.

======================================
//app.java
public class app
{
        static
        {
                System.loadLibrary("so1");
        }
        native void so1Func();  

        public static void main(String args[])
        {
                app myApp = new app();
                myApp.so1Func();
        }

        javaFunc()
        {
                System.out.printfln("In Java, called back from so1.so");
        }

}
======================================
//so1.c for libso1.so
JNIEnv gEnv;
jobject gObj; 

void Java_app_so1Func(JNIEnv * jEnv, jobject _this)
{
        gEnv = jEnv;
        gObj = _this;
        ...
        library = dlopen("./libso2.so", RTLD_LAZY);
        funcRef = dlsym(library, "so2Func");
        (*funcRef)(); //call function so2Func() in libso2.so
        ...
}

void callBack()
{
        jclass cls = (*gEnv)->GetObjectClass(gEnv, gObj);
        jmethodID mid = (*gEnv)->GetMethodID(gEnv, cls, "javaFunc", "(V)V");
        (*gEnv)->CallVoidMethod(gEnv, gObj, mid); //call back to java app.
}
======================================
//so2.c for libso2.so
so2Func()
{
        while(1)
        {
                ...
                //********************************
                //how to call back to the function 
                //callBack() in libso1.so from here?
                //********************************
                ...
        }
}

app.java loads libso1.so into its address space, and libso1.so loads
libso2.so into the same address space.  So, libso2.so should be able to call
a function in libso1.so (in the same address space) if libso2.so can resolve
that function's symbol, but how?

Thanks.


Lee


-----Original Message-----
From: Nathan Meyers [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 02, 2000 10:44 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: JNI & .so Files


[EMAIL PROTECTED] wrote:
> 
> Hi,
> 
> I got a question on JNI.  It would be appreciated if someone could help.

Lee,

I'm having trouble understanding the problem you're posing. What is the
difficulty you're trying to solve... how to call functions in one .so
from another .so? You're already doing that.

Is it the problem of figuring out how to call a function whose name you
do not know until runtime? If so, I think you'll find the
dlopen()/dlsym()/dlclose() functions useful. Other than that
possibility, I'm not quite sure what problem you're trying to solve.

Nathan


> 
> Q:
> In order to hide java and jni related issues (e.g. jni function name
> convention, etc.) from .so programmers, a wrapper .so file so1.so is used
in
> between java app and another .so file so2.so (the one with native
functions
> we need).  This way, the java app interacts with the wrapper so1.so
> directly.  Every time when java app needs a native function service in
> so2.so, it first calls a native function in so1.so through jni.  so1.so,
in
> turn, calls the required function in so2.so.  Everything works fine in
this
> direction (i.e. java app->so1.so->so2.so.  java app loads so1.so, and
so1.so
> loads so2.so, then java app invokes a function in so2.so through a
function
> in so1.so).  The question is how a function in so2.so calls back to java
app
> through a function in so1.so?  In fact, we only need to know how functions
> in so2.so can call functions in so1.so because jni will let us call back
to
> java app from so1.so.
> 
> More information:
> - the java app is multithreaded application using Thread.start().
> - each java thread may call the same function in so1.so, then
>   this function, in turn, calls a function in so2.so
> - native code doesn't create any thread.
> 
> Use IPC? which one is easier on Linux?  Is there any other mechanism
> for this on Linux?
> 
> A simple sample code will help a lot.
> 
> Thank you.
> 
> Lee
> 
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to