Calling C Functions in existing DLL's from Java

Hi Guys ,The tutorial in this site talks about creating ur own DLL's and then calling them from Java . I need to call C functions in existing DLL's from Java . How do I go about doing this ? . Any help on this would be much appreciated.regardsmurali
[293 byte] By [murali79] at [2007-9-26 9:29:25]
# 1

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);

}

}

HJ-ICT at 2007-7-1 20:52:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

>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.

jschell at 2007-7-1 20:52:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
I have heard so much about it, but never seen any code. The person who can give me an example of a very simple DLL wrapper, lets say for a delphi DLL, will get serious DUKE DOLLARS and will be an absolute HERO in my Books.IN OTHER WORDS PLEASE HELP
JonathanJ at 2007-7-1 20:52:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

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. :-)

ralphc at 2007-7-1 20:52:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
Is there a site or book where I can learn how to program a very simple Regular DLL in C / C++And then also a wrapper DLL ?
JonathanJ at 2007-7-1 20:52:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
Hi,I suggest not to write the JNI DLL for a regular DLLyourself: there is a number of generators which dothat for you. One of the famous ones is Swig whichincludes a Java generator module.see http://swig.sourceforge.netHave fun,wiedkla
wiedkla at 2007-7-1 20:52:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7
Yes you can.Write one wrapper dll . The java code will call one api which will be defined inside the wrapper dll and wrapper dll will in turn call the existing dll api.
ncpsk at 2007-7-1 20:52:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...