I am currently working on a project that involves injecting a DLL into a Java process, the code in my DLL makes a jni->DefineClass call to create a new Java class with a few functions, one of these functions calls back to native code.
My Java class looks like this:
class myDefinedClass{
public static native void native_test();
public static void nativeWrapper(){ //give this to interpreter
native_test(); //interpreter calls this code
}
}
I have done tests and know for a fact that the class is properly defined, I am able to then call the Java functions inside of it using JNI, however I have encountered a problem with the native_test() function, it does not seem to link to the JNIEXPORT function within my DLL.
My JNIEXPORT function was generated with javac -h . ./test.java, and looks like this:
extern "C"{ //also tried without the extern "C"
JNIEXPORT void JNICALL Java_myDefinedClass_native_1test(JNIEnv *env, jclass cls) {
printf("JNI CALLED FROM JAVAn");
while(true){} //an infinite loop to give some indication that the code runs by freezing the thread, dosnt work.
}
}
My jni code to call the java code is:
jclass cls1 = env->FindClass("myDefinedClass");
jmethodID test1122 = env->GetStaticMethodID(cls1, "nativeWrapper", "()V"); //calling both functions dosnt reach the jniexport
env->CallStaticVoidMethod(cls1, test1122);
env->DeleteLocalRef(cls1);
After some debugging, I was able to use jni->ExceptionDescribe(); to find the issue, its:
java.lang.UnsatisfiedLinkError: myDefinedClass.native_test()V
This issue happens when calling the function directly from jni, and indirectly with nativeWrapper(). I am able to call nativeWrapper() with jni, it runs fine, the problem is native_test() not getting linked to the JNIEXPORT within the injected dll.
So in summery: An injected dll defined a class, and this class should call back to the native code, but there is some sort of issue with the defined class not finding my JNIEXPORT function.
I have tried calling the function indirectly (like with the nativeWrapper).
I also tried detaching and re-injecting the dll to see if having the JNIEXPORT be loaded after the class was previously defined would fix the issue, this did not work either.