DLL Loading in JAVA

I have a DLL in win32 and i need to execute its function from JAVA. I dont not have the codes to the DLL.

Please provide sample codes with some explaination.
hkfungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbormannCommented:
first off if u have any problem pls ask.
u can read the following good links

look

http://www.experts-exchange.com/Programming/Languages/Java/Q_10211064.html

but i think that this will solve ur problem.

http://www.experts-exchange.com/Programming/Languages/Java/Q_10081643.html

copying text from there to here so u save 10 points.
:-)
 
Previously Asked Java Programming Question
 
From: angelany  Title: "Writing Java code using native methods in dll file"  
Points: 10 Points  Date: Thursday, September 17 1998 - 08:35AM PDT  
 
Hi,

I am working on WindowsNT 4.0 and need to write Java code that invokes a computation core already implemented in C,
but the core is only available as a dll file, no source
code.

I read about JNI on Sun's Java tutorial, but that would
require re-writing original C functions such that the signature matche the header file declaratoins. This
header file is generated by "javah -jni MyFile.java" and
its signature is fixed. And since I don't have the option
to touch the core dll implementation, I can't make the
match.

How can this be done?

Any help is appreciated.

Angelany
 
 
Accepted Answer
 
 From: msmolyak
 Date: Thursday, September 17 1998 - 09:45AM PDT  
 


Text below...
 
 
 
   Question History  
 
Accepted Answer
 
 From: msmolyak
 Date: Thursday, September 17 1998 - 09:45AM PDT  
 
You can do it as long as you have an API to the third party DLL. All you have to do is to create your own DLL which contains native implementations of Java methods where C or C++ routines will have signatures generated by javah. All those routines would do is to invoke the appropriate functions in the target DLL with known API. I did it earlier for ODBC  (where ODBC driver manager was the third party DLL) and we created our own DLL calling the ODBC methods.
 
 

Comment
 
 From: angelany
 Date: Thursday, September 17 1998 - 10:26AM PDT  
 
msmolyak,

Thanks for your answer. There are still a few things that I
don't quite understand. Would you please elaborate a little more?

Suppose I have these files:
test.dll -- the core dll file
TestMain.java -- the test application in Java, it calls class and methods in "Rate.java"
Rate.java -- the Java file that contains definition of native methods
Rate.h  -- generated by "javah -jni Rate.java"
Rate.c  -- generated by "javah -stubs Rate.java"

Now, should I write a code called something like "RateImp.c"
to implement all the signatures in "Rate.h"? How are the Java
specific types handled? How does the "Rate.c" come into play?

Thanks.

Angelany
 
 

Comment
 
 From: msmolyak
 Date: Thursday, September 17 1998 - 10:33AM PDT  
 
First of all you do need stubs if you are using JNI, stubs were needed for the old native interface. Thus you just take Rate.h and create a C file (e.g., RateImp.c) where you will implement the methods according to their signatures. You do not need your Rate.c
 
 

Comment
 
 From: angelany
 Date: Friday, September 18 1998 - 09:48AM PDT  
 
Hi msmolyan,

I still have trouble passing data types to and from Java.
Could you please give an example on passing an object and
a string? Thanks a lot.

Angelany
 
 

Comment
 
 From: msmolyak
 Date: Friday, September 18 1998 - 10:41AM PDT  
 
Here is an example. The first two parameters (JNIEnv and jobject) are present in all teh calls. The first is the environment, the second is pointer to the invoker object. In this example I pass file name as a parameter and convert it from Java string to C string. Then I invoke a C function using that string.

Go to the JNI Spec and tutorial on Javasoft's Web site, a lot of useful info.

/**
 * This function opens specified configuration file for reading or writing.
 */
JNIEXPORT void JNICALL Java_com_excalib_rw_CfgObj<wbr />ectAdapter<wbr />_openConfi<wbr />gFiles
  (JNIEnv * env, jobject obj, jstring jFileName)
{
char *cfgFileName;

// Convert Java string to C string
cfgFileName = (*env)-&gt;GetStringUTFChars(<wbr />env, jFileName, (jboolean)NULL);

// Call RetrievalWare API function openning configuration file.
cq_open_conquest_cfg(cfgFi<wbr />leName, FILE_OPEN_FOR_READ);

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbormannCommented:
Oops forgot to paste URL's

Apart from the JNI tutorial from Sun at
http://java.sun.com/docs/books/tutorial/native1.1/index.html

,see from a PAQ at EE ,again pasting from there to here.
from http://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=10203703 

See these
http://www.math.tau.ac.il/system/java/jdk1.0.2/tutorial/native1.1/ 

This is one of the Best and I used it at Start and also now....
http://home.pacifier.com/~mmead/cs510jip/jni/ 

http://www.itlibrary.com/reference/library/1575212986/htm/ch32.htm 

look at CodeGuru's Links to other good sites.
http://codeguru.developer.com/java/JNI/index.shtml 
 
http://www.cs.pdx.edu/~antoy/Courses/Advanced/units/jni/ 

http://www.swtech.com/java/native/ 

this one is repeating everything they tell but for beginners it looks good.
  http://herzberg1.ca.sandia.gov/JavaCourse/Java_1_1-Week5.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.