jjescri
asked on
dll inside java
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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++
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++
ASKER
Hi wex,
when I do 'java testp' an error is returned.
java.lang.UnsatisfiedLinkE rror: add2
at testp.main<testp.java:8>
when I do 'java testp' an error is returned.
java.lang.UnsatisfiedLinkE
at testp.main<testp.java:8>
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("native dll");
}
}
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)->GetDoubleArrayElem ents(env, myarray, &isCopy);
/* now here invoke your dll function since you have the
double[] array in localArray */
double *result = func(localArray);
(*env)->ReleaseDoubleArray Elements(e nv, 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)->GetDoubleArrayElem ents (RetArray, NULL);
for ( x = 0; x < len; x++)
localArray[x] = result[i];
(*env)->ReleaseDoubleArray Elements(R etArray, 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
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("native
}
}
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
localArray = (*env)->GetDoubleArrayElem
/* now here invoke your dll function since you have the
double[] array in localArray */
double *result = func(localArray);
(*env)->ReleaseDoubleArray
/* 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)->GetDoubleArrayElem
for ( x = 0; x < len; x++)
localArray[x] = result[i];
(*env)->ReleaseDoubleArray
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
ASKER
Thanks evijay
ASKER
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'.