Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java native keyword and dll usage

 
Lorenz Baylon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
I am using a dll(written in C or C++, im not so sure) for my application, I need to use the dll's methods from my Java program. I've learned that a way (or maybe the only way.. any comments?) to do this is to use java's 'native' keyword. My problem is on how to get the dll's method names and how exactly should i deal with them. Is there some workaround in java to do such task of knowing a dll's method names? Or maybe i should ask for some pointers first regarding the usage of 'native'?

Thank you in advance

lhorenz
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create a java native wrapper DLL that will serve as the bridge between the java application and the C/C++ DLL.
The java methods tagged as "native" will call the JNI wrapper DLL functions,
which in turn will call the C/C++ DLL functions.

Regards
 
Lorenz Baylon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response, do my java methods need to be named exactly the same as the C/C++ DLL functions?

Is there a way that I know the C/C++ DLL function names?

Again, many thanks
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read a JNI Tutorial first to get acquainted with the beast :-)

To answer your questions, the JNI functions must follow a specific naming convention related to the java part, NOT the third party C/C++ DLL part.
For instance, suppose you have a java class with one native method:
package a.b.c;
class MyBridge
{
native void myMethod();
}
Then the JNI function name MUST be:
Java_a_b_c_MyBridge_myMethod

If you don't have any documentation on your third party C/C++ DLL, it will be hard to know how to use it.
Knowing the exported function names of the DLL will not help much.
There are many tools out there to read all the exported symbols of a DLL.
But you might find one in the C/C++ build tools you are currently using.

Hope this help.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you can avoid having to use the big long Java-mandated function names, if you explicitly connect your C functions to the Java methods, via the JNI function RegisterNatives(). However, it is unlikely that this will allow you to call the existing C functions directly from Java. You'll probably still need "bridge"/"trampoline" methods, to adjust the argument types etc.
 
Lorenz Baylon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi thanks for the responses, i guess now i have some hints in getting along with 'the beast' (haha i like that)...

those responses really help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic