Call .dll from Java

Hi All,

I have .dll file, which does encryption and decryption of a password.

This dll was earlier used in .net to encrypt and decrypt the password.

But now i am building the same application using J2ME, where i need to call this dll to encrypt and decrypt the password. Now i have already have an existing database which is MS SQL Server 2000. When i try to Login through the system i need to check the database whether it is valid or not. Since the database is already have encrypted password.

I just need to call the .dll from java and need to find out whether the encrypted data is correct or not.

So, i need to understand how to call this .dll from my java file, also, how to call the decrypt function from this .dll in my java class.

Since, i am very new to this area, Please guide me in this context.

Thanks & Regards,

Arvind

[893 byte] By [arvind.m@mgl.coma] at [2007-11-26 15:46:53]
# 1
Use JNI
bschauwejavaa at 2007-7-8 22:06:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Hi,

I have followed the steps to create the header file then the .c file.

Also, downloaded the MinGW software to compile the file.

i am tryint to compile the file by providing the following command

E:\MinGW\bin>gcc -c -l"C:\Program Files\Java\jdk1.5.0_08\include" -l"C:\ProgramFiles\Java\jdk1.5.0_08\include\win32" - o "C:\calldll\MPmisDll.o" "C:\calldll\MPmisDll.c"

but i am getting the following exception.

C:/calldll/MPmisDll.c:1:17: jni.h: No such file or directory

In file included from C:/calldll/MPmisDll.c:2:

C:/calldll/MPmisDll.h:2:17: jni.h: No such file or directory

In file included from C:/calldll/MPmisDll.c:2:

C:/calldll/MPmisDll.h:15: syntax error before "void"

C:/calldll/MPmisDll.h:16: parse error before '*' token

C:/calldll/MPmisDll.h:16: warning: data definition has no type or storage class

C:/calldll/MPmisDll.c:6: syntax error before "void"

C:/calldll/MPmisDll.c:6: parse error before '*' token

Could you please tell me how to compile my .c file. So that i can create the .o file.

Thanks,

Arvind

arvind.m@mgl.coma at 2007-7-8 22:06:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Looks like a problem in where you told gcc it would find jni.h. Check that the file is where you said it would be.
bschauwejavaa at 2007-7-8 22:06:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

Hi bschauwejava,

For better understanding i will show some steps which i have done.

My Java File:

public native void encryptDll();

static {

System.load("C:/calldll/PPED.dll");

System.out.println("Loaded");

}

public static void main(String[] args) {

new MPmisDll().encryptDll();

}}

My header File by running the command javah -jni MPmisDll

/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class MPmisDll */

#ifndef _Included_MPmisDll

#define _Included_MPmisDll

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:MPmisDll

* Method:encryptDll

* Signature: ()V

*/

JNIEXPORT void JNICALL Java_MPmisDll_encryptDll

(JNIEnv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

Then My C file :

#include <jni.h>

#include "MPmisDll.h"

#include <stdio.h>

#include <string.h>

JNIEXPORT void JNICALL Java_MPmisDll_encryptDll(JNIEnv *env, jobject obj)

{

printf("Hello world!\n");

return;

}

Then i am trying to compile this C file using the command:

C:\calldll>gcc -c -l"C:\Program Files\Java\jdk1.5.0_08\include" -l"C:\ProgramFiles\Java\jdk1.5.0_08\include\win32" -o "C:\calldll\MPmisDll.o" "C:\calldll\MPmisDll.c"

Then i got the error as i had posted earlier...So..Pelase let me know if their is any other steps i have missed out.

I have included the dll in same folder as well as Windows\System32.

Please let me know if i need to set anything in CLASSPATH or PATH in evironment varaible..

Thanks & Regards,

Arvind

arvind.m@mgl.coma at 2007-7-8 22:06:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
Your gcc instruction shows two "-I" directories: places where the compiler should look for jni.h.Is jni.h in one of those directories?
bschauwejavaa at 2007-7-8 22:06:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

hi

try this commands..

I have did this in cygwin setup in the windows platform

javac -classpath . HelloWorld.java -->this command creates .class file..

javah -classpath . -jni HelloWorld -->this command creates C header file..

gcc -mno-cygwin -I /usr/include -Wl,--add-stdcall-alias -shared -o HelloWorld.dll HelloWorld.c --> this command creates " HelloWorld.dll "

java -cp . HelloWorld--> this command runs the java calling the function on C program through jni.

Message was edited by:

VaRadharajan

VaRadharajana at 2007-7-8 22:06:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

Hi,

I have MinGW to compile the C file, I was able to compile the class using the command

For compiling C file

gcc -c -I"C:\Program Files\Java\jdk1.5.0_08\include" -I"C:\Program Files\Java\jdk1.5.0_08\include\win32" -o "C:\calldll\MPmisDll.o" "C:\calldll\MPmisDll.c"

****************************************************************************

For creating .def file compile using this command

E:\MinGW\bin>gcc -shared -o"C:\calldll\MPmisDll.dll" "C:\calldll\MPmisDll.o" "C:\calldll\MPmisDll.def"

******************************************************************************

after that i tried running my java file, but still get the linker error

C:\calldll>java MPmisDll

Loaded

Exception in thread "main" java.lang.UnsatisfiedLinkError: encrypt

at MPmisDll.encrypt(Native Method)

at MPmisDll.main(MPmisDll.java:21)

Also, tried using

C:\calldll>java -cp . MPmisDll

Loaded

Exception in thread "main" java.lang.UnsatisfiedLinkError: encrypt

at MPmisDll.encrypt(Native Method)

at MPmisDll.main(MPmisDll.java:21)

Still get the same results. Do we have any other steps which i have to try?

Thanks & Regards,

Arvind

arvind.m@mgl.coma at 2007-7-8 22:06:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...