Solved

dll inside java

Posted on 1998-05-18
6
807 Views
Last Modified: 2012-06-21
Hi,
I have a dll called mydll.dll.
This dll have a function which header is:
double *func(double *data).

Both pointers points to arrays of six double.
How can I call 'func' inside a java class?
Please, give me a detailed sample code.

Thanks.
0
Comment
Question by:jjescri
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
wex earned 100 total points
ID: 1221159
This is a JNI problem.  The solution is split into two halves.  First, you need some class to load the dll.  That's easy.  One class (foo) with one routine that does a System.loadLibrary("yourDLL");

Then to use the routine inside the DLL, you make another class which creates an instance of foo.  You can then call instance.func() with the appropriate args.

Now the Q is what are appropriate args.  I'd suggest using Vectors.  So something like:
      Vector results = instance.func(rgs)
where rgs is also a Vector.

Then you can pull the elements out by iterating over the Vector and casting each member to be a Double.  If you really need the double type, you can get Double.doubleVal()

That make sense?
0
 

Author Comment

by:jjescri
ID: 1221160
Hi wex,

I have a DLL  called 'p' with a function 'add2':

int add2(int v){return (v+2);}

and when I compile the next code javac return an error.

class pjava
{
      public pjava()
      {
            System.loadLibrary("p");

      }
}


class testp
{
      public static void main(String args[])
      {
            pjava e=new pjava();
            int r;
            int s=2;
            r=e.add2(s);

      }
}

error: 'add2' is not a member of class 'pjava'.
0
 
LVL 1

Expert Comment

by:wex
ID: 1221161
OK, change your pjava class to the following:

public class pjava
{
   public native int add2 (int v);

   static {
      System.loadLibrary("p");
   }
}

Also, you need to make sure that java can actually find the DLL for runtime.  On UNIX this
would be done by changing the LD_LIBRARY_PATH variable.  On PCs I use Symantec Cafe, which has an option to set the library path on a per-project basis.  I suspect you'll need to do something similar in VJ++
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jjescri
ID: 1221162
Hi wex,
when I do 'java testp' an error is returned.

java.lang.UnsatisfiedLinkError: add2
       at testp.main<testp.java:8>

0
 
LVL 4

Expert Comment

by:evijay
ID: 1221163
Hi,

I think you both are missing something fundamental here.
The solution will be

Write a wrapper java class with a native method as follows

public class DLLCallingClass {
public static native double[] func(double[] inpArra);
static {
  System.loadLibrary("nativedll");
}
}

Now, coming to how to generate nativedll.

compile java program

javac DLLCallingClass.java

Then, generate jni header file

javah -jni DLLCallingClass

This creates a file DLLCallingClass.h

now write a c program with name
DLLCallingClass.c

extern double *func(double *);

#include "DLLCallingClass.h"

JNIEXPORT jdoubleArray JNICALL Java_DLLCallingClass_func
  (JNIEnv *env, jclass myclass, jdoubleArray myarray)
{
       jsize length;
            jboolean isCopy;
            jdouble *localArray;
            jint i;

            length = (*env)->GetArrayLength(env, myarray);
            localArray = (*env)->GetDoubleArrayElements(env, myarray, &isCopy);

/* now here invoke your dll function since you have the
   double[] array in localArray */
            double *result = func(localArray);

            (*env)->ReleaseDoubleArrayElements(env, myarray, localArray, 0);      
 
/* convert back the result to java double array !!! */
/* how you are going to get this len parameter below is upto you.
you might know how many elements the func(double *) will return.
*/
      jdoubleArray RetArray = env->NewDoubleArray(len);

       jdouble *localArray = (*env)->GetDoubleArrayElements (RetArray, NULL);

       for ( x = 0; x < len; x++)

               localArray[x] = result[i];

                             (*env)->ReleaseDoubleArrayElements(RetArray, localArray, 0);

                             return RetArray;
}

Please see that all the declarations of the variables is at the beginning of the program in above segment. (I am using C++ so i neednt).

To invoke the method from the dll, you must have the .lib file which you must link with this file to generate the final new dll
nativedll.dll (which is used in system.loadlibrary call)

Now you might have got a overall picture of what is going on.

Your java class invokes a c/c++ method by name
Java_DLLCallingClass_func but not just func.
This method inturn converts java double array to c/c++ double array, invokes the actual dll function,  packs the result into again the java double array and returns.

For more info
see

http://www.developer.com/reference/library/1575212986/htm/ch32.htm
0
 

Author Comment

by:jjescri
ID: 1221164
Thanks evijay
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
json example 39 135
Eclipse IDE - Cannot copy/paste from console output 8 136
Error with Java/Cache JDBC Classpath 2 31
type mismatch (Object[] to double[] 4 14
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now