Define a class you want to access the DLL, declare native methods in it, generate C++ headers from the class with javah, call your DLL in methods you generated headers for, build second DLL, call it from java.
What should I use to write and compile .cpp files? I have tried to use Rhide with DJGPP, but it seems to me that it cannot compile things around JNI. Do I have to use Microsoft Visual C++?
Those keywords specify calling conventions (parameters read leaf to right, caller clears stack etc.) You'll have to read the doc for both compilers to find the equivalents. I'm a bit suprised that there aren't macros in the gcc distribution to deal with this. I'd look at the gcc doc/web site for any advice on porting VC code to gcc.
I am not ready with my studies of gcc an cygwin but I will give you, what I have already now.
May be it works for you.
$ gcc -Wall -mno-cygwin -Wl,--add-stdcall-alias -D_JNI_IMPLEMENTATION_ -I/cygdr
ive/c/j2sdk1.4.2_04/include -I/cygdrive/c/j2sdk1.4.2_04/include/win32 -shared yourfile.cpp -o yourresult.dll
-Wall
switch on all warnings -mno-cygwin
Switch off the use of the standard cygwin libraries (posix-standard) -Wl,--add-stdcall-alias
Advises the linker to add standard calls to the export as alias. -D_JNI_IMPLEMENTATION_
Defines the JNI_IMPLEMENTATION -switch (used inner jni.h as ifdef switch).
#ifdef JNI_IMPLEMENTATION
#define JNI_IMPORT_OR_EXPORT JNIEXPORT
#else
#define JNI_IMPORT_OR_EXPORT JNIIMPORT
#endif -shared
Advises the compiler to produce a shared library, this obsoletes the -dll switch.
Look at
http://forum.java.sun.com/thread.jsp?forum=4&thread=5500
Because I want to earn my first Duke Dollars I will give you a further hint:
http://forum.java.sun.com/thread.jsp?forum=52&thread=471668
I hope you get all you need!
public static void main(String[] args) {
new HelloWorld().Hello();
}}
use load() insted of loadlibrary() and give the absolute path of dll in the load function
2.))
now compile this you will get a HelloWorld.class file
it will show loaded and a error message UnsatasifiedLinkError
on the Hello() Native method
there is no need to worry about continue ahead you will find why it is showing this when you be able to run this
3.))
now Use javah command on a command prompt to generate the .h file
javah HelloWorld it will give you a .h file .h file will look like this
/ DO NOT EDIT THIS FILE - it is machine generated /
#include <jni.h>
/ Header for class HelloWorld /
Sample example given in many of the sites are very simple in the sense that you create a dll of your own and invoke a function in it. Initial question in this thread is that how to use an already existing dll say X.dll. Following are the steps
1. We need to create an intermediate JNI wrapper, which is a pure C libray say Y.dll, which internally invokes X.dll.
2. Load Y.dll file in your java class and invoke the function.
Well, currently I am working on this. I am able to get through the first step. In the second step, I am able to load Y.dll file but when I invoke a function JVM terminates because it is not able to locate X.dll referenced by Y.dll.