calling a C-dll function from java code
-
Hello, I am very new to java. I want to call a C-dll function from my java code and I don't know how. Can you please help? Thanks. Where there is a WISH, there is a WILL.
There are facilities for this kind of thing in the MS version of the java engine. It's documented on the microsoft java site. Basically microsoft have put in some naughty java extensions for this purpose. You do things like
/** @dll.import("USER32") */ static native int MessageBox(int hwndOwner, String text, String title, int style);
(This must be in a class definition, of course). I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant. -
There are facilities for this kind of thing in the MS version of the java engine. It's documented on the microsoft java site. Basically microsoft have put in some naughty java extensions for this purpose. You do things like
/** @dll.import("USER32") */ static native int MessageBox(int hwndOwner, String text, String title, int style);
(This must be in a class definition, of course). I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant.Malcolm McMahon wrote: I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant. Yes, there is. It's called Java Native Interface.
-
Malcolm McMahon wrote: I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant. Yes, there is. It's called Java Native Interface.
That doesn't look like it would allow you to access an existing DLL though. Only one specially written for this purpose. Of course you could write an interface from there to a standard DLL.
-
That doesn't look like it would allow you to access an existing DLL though. Only one specially written for this purpose. Of course you could write an interface from there to a standard DLL.
That's right. I found JNI but it sounds like it is very slow. And you can't use existing dll. 1.You write the native methods declaration in a java class 2.Then you compile the java class (javac) 3.Generate a header file from this class using javah -jni ... 4.include this header in you dll project and provide and implementation for the generated header functions. 5.Rebuild your dll From this point, you can use the dll via the java class methods we wrote at step 1. I tried using JNI. It works because I could change the source of the dll and I wanted to use. But it is surprisingly slow. Now I am looking forward to using sockets. Thank you. Where there is a WISH, there is a WILL.