This is how to call NativeTest.dll
In NativeTest I call the methods show() and getLine(String,int)
class NativeTest
{
static
{
System.loadLibrary("NativeTest");
}
public native void show();
private native String getLine(String prompt, int[] lijst);
public static void main(String[] args)
{
NativeTest nt = new NativeTest();
nt.show();
String invoer = "Javatest";
int[] lijst = {1,2,3,4,5};
String uitvoer = nt.getLine(invoer,lijst);
System.out.println(uitvoer);
}
}
>need to call C functions in existing DLL's from Java . How do I go about doing this ?
You can't.
You can however, write your own dll which calls those existing functions. And your dll would be written to support the Java Native Interface specification which allows a java method to be implemented as a C method.
What you are interested in can be done with what's called "shared stubs", from the JNI book (http://java.sun.com/products/jdk/faq/jnifaq.html), although you don't need the book to do it (I didn't).
The example code will call functions with any number and kind of parameters, but doing that requires some assembly language. They supply working examples for Win32 (Intel architecture) and Solaris (Sparc).
If you can limit yourself to functions to a single function signature (number and types of parameters), or at least a small set that you know you'll call at compile time, you can modify the example so that the assembly language part isn't needed, just straight C code.
Then you'll have one C library that you compile and a set of Java classes and you can load arbitrary functions out of arbitrary dynamic libraries. In my case you don't even have to know what the libraries and functions are ahead of time, the user can set that up in a config file.
You mentioned doing this with Delphi. One thing to watch out for is C versus Pascal (Win32) function calling convention. A good rule of thumb; if it crashes hard, you probably picked the wrong one, try the other. :-)