accessing a method through its mapping address

Hi,

I'm wrapping a native C dll which has a function asking for function pointer as a parameter. In fact, it's a way to register a callback that will be called on event.

My need is to wrapp this function so that i can register a Java method instead of a C function.

I could, in my wrapper (written in C as well), create a function that would itself call the Java method with one of the JNI useful functions, and then pass the address of that new function to the callback register ; but i am not supposed to know in advance the method which will be attached, and more than that i want to register any amount of callbacks, and so this is not known by the time the wrapper is being written.

Therefore, the only way i see would be to find a way to call a Java method by its address mapped in memory (as i would do with a C function).

As far a i know, the reference address of Java methods are not supposed to be visible to the developer. More than that, i don't know the way the JVM mapps binary code in memory.

Does anyone know if it is possible to get the address where a java method is mapped in memory (static and/or not static method) ?

If this address is known, would there be any way to launch this method from C native code?

Thanks in advance.

mca...

[1318 byte] By [_mca63a] at [2007-11-26 12:54:57]
# 1
1. ON what object would you call this java method if you could find it?2. What kinds of parameters would you pass this methid?Unless yu have arguments that will help you determine these things, you ar probably wasting your time trying to call directly into java.
bschauwejavaa at 2007-7-7 16:47:33 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Hi,

You need to register a callback.

You can do this with JNative (a free LGPL generic wrapper).

You can take a look at JNative's sources at :

the callback itself : http://jnative.cvs.sourceforge.net/jnative/JNative/src/java/JNative/org/xvolks/test/callbacks/EnumCallback.java?view=markup

how to use the callback : http://jnative.cvs.sourceforge.net/jnative/JNative/src/java/JNative/org/xvolks/test/callbacks/TestCallback.java?view=markup

If you MUST do it by yourself see :

the asm declaration : http://jnative.cvs.sourceforge.net/jnative/JNativeCpp/CallBack.h?view=markup

how to use it : http://jnative.cvs.sourceforge.net/jnative/JNativeCpp/CallBack.cpp?view=markup

--Marc (http://jnative.sf.net)

mdentya at 2007-7-7 16:47:33 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

Hi and thanks,

I already had a look there and found this sentence "This library allows developers to access native libraries (DLL and lib.so) from java", so i thought it's not what i'm looking for since i wish to make the reverse (call Java code from C dll).

Anyway, I'm gonna have a closer look at the JNative sources,

thanks all guys.

ps : i had login problem and had to change it.

__mca63a at 2007-7-7 16:47:33 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

I'm having a bit of difficulty with this, too. We've written a DLL to allow us to access a device's SDK from Java. JNI calls in to the DLL work great. But, we also want to be able to make calls in to Java from the C code in the DLL.

We've found that if we call like this, it works fine:

java method --> DLL (via JNI) --> another java method

But, we can't seem to make calls in to java when the stack initiates in the DLL:

DLL (via JNI) --> another java method

(essentially, we're trying to pass along the device's callback in to Java):

Every time, we get some kind of access exception way up the stack when we're running the code. We're pretty new to JNI, especially trying to use it to make calls back in to Java. If anyone has some quick advice, it would be very much appreciated.

Without boring you with a lot of details, we do this in our C function:

//(cached_jvm is the JVM object we received on the init):

JNIEnv *env;

(cached_jvm)->AttachCurrentThread((void **)&env,NULL );

jclass cls = env->FindClass("com/merl/forlines/input/fingerworks/FingerWorksDeviceJNI");

javaMethod = (env)->GetMethodID(cls, "cliffCallback", "(I)V");

// '17' here is the value we're trying to pass to our method, which takes an

// int. TheObject is the the java object we receive when a call to our 'init'

// function is made

env->CallVoidMethod(TheObject, javaMethod, 17);

(cached_jvm)->DetachCurrentThread();

dwigdora at 2007-7-7 16:47:33 > top of Java-index,Java HotSpot Virtual Machine,Specifications...