2

I have a .dll library which exports a function in the following format:

_Java_folder1_folder2_folder3_JavaClassName_javamethodname@16

I cannot modify the .dll. It was given to me.

I need to write a java program to call the native javamethodname. However, I have trouble generating the right header file. How can I generate a header file with the signature specified above?

I tried javah -jni JavaClassName, but it does not give me the desired result. When I call javah from inside the directory hierarchy, I get a header like:

Java_JavaClassName_javamethodname

When I try to call javah from outside the directory hierarchy, I get an error.

Also, how do I get the underscore in the header signature in front of Java? That is: _Java not Java.

Thanks!

user207421
  • 305,947
  • 44
  • 307
  • 483
skyknight
  • 185
  • 2
  • 10
  • 1
    Why? You already have the Java class and the DLL. That's all you need. Just write Java code that calls the Java class. – user207421 Jun 20 '12 at 20:12
  • Yeah, why do you need the header? What you need is the Java native class, which you should be able to extract from whatever JAR file calls the DLL. – Hot Licks Jun 20 '12 at 20:40
  • I managed to call the .dll. However, there seems to be some software protections in there, which check whether certain variables are initialized and whether certain classes are loaded. Seems like there is also more to it, cause I can't get the expected output :( – skyknight Jun 21 '12 at 22:13

3 Answers3

1

Put your Java class in package folder1.folder2.folder3.

The C source code will be named "Java_folder1_folder2_folder3_JavaClassName_javamethodname", without the underscore.

You'll need to determine what parameters the native method is expecting.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • how to call javah on the package? how to get the leading underscore in front of Java, like _Java? Thanks! – skyknight Jun 20 '12 at 19:25
  • @skyknight -- Running `javah` will do you no good. `javah` operates against the Java native class to produce a .h file to compile the native method. You already have the compiled native method. – Hot Licks Jun 20 '12 at 21:35
  • In the javah command you'll also need to fully qualify the classname with the package. I think you'll see the underscore when you build a DLL. That said, you already have a DLL. Why do you need a header file if you already have the DLL? – Andy Thomas Jun 20 '12 at 21:36
  • @AndyThomas-Cramer , regenerating the header file helps in debugging. Otherwise, when the code fails to run, there's very little information I get back. – skyknight Jun 21 '12 at 22:11
1

Writing the java code is easy: you create a Java project with package name folder1.folder2.folder3 and put your class JavaClassName in there with a native javamethodname. You don't need to generate headers or anything, that has been already done by someone who created the DLL and had the same class as you are reconstructing.

The Java_ prefix is standard and is added by the JVM-JNI linker, you don't need to care about it.

However, i read between the lines that you have also a problem with the leading underscore. It is a "compiler decoration" and if you got only the DLL (no sources, no recompilation), you might be as well doomed. Various compilers add various "decorations" to ensure that you don't mix up calling convention between the caller (JVM) and callee (DLL). It has some observable default behavior and can be mangled by various compiler options and/or definition files (.def on MSVC). Neither of this is applicable for your case. So you need:

  1. Find out what compiler was used to produce the DLL
  2. Look up the calling convention for which the given compiler produces your observed symbol decoration
  3. Your DLL is runnable only through JVM which has the same calling convention defined as JNICALL macro in $JAVA_HOME/include/your_platform/jni_md.h
Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
0

If the DLL doesn't already come with a corresponding Java class wth native method declarations it is incomplete and probably 100% useless. You shouldn't be expected to reverse engineer this. It may even have been built incorrectly. I would be asking for evidence that it works before I went any further with this. That evidence must include the Java part; otherwise it isn't evidence.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
user207421
  • 305,947
  • 44
  • 307
  • 483
  • the .dll runs fine when it is called from an eclipse plug-in. Hence, it is fine. The problem is that I have to write a command line interface for it. Eclipse does some weird things. – skyknight Jun 20 '12 at 19:50
  • 1
    @skyknight If you already have the DLL and the Java class I don't understand why you need to regenerate anything. – user207421 Jun 20 '12 at 20:06
  • actually regeneration helps to debug easier. If the header wouldn't match the expected function call in the .dll, it wouldn't run. – skyknight Jun 21 '12 at 22:08