J2se: JNI: Java calls an existing dynamic connection library (DLL)

Source: Internet
Author: User

Sometimes, a project is not made up of a single language, so collaboration between languages cannot be avoided.
For example, if your project is written in Java, You need to encrypt and decrypt the data. Generally, you will use Java to implement an encryption algorithm. However, now you have a non-Java component on your hand that can be used, such as DLL. Considering the efficiency, you can use this DLL better. In this case, you need to use JNI (Java Native Interface)-Java Native Interface.

In this case, let's start with calling an existing DLL example in Java.

The Des. dll call interface is as follows:

1. Encryption

I = encrypt (object, ciphertext, plaintext, key)

I = 1, the encryption is successful; I = 0, the encryption fails; I is an integer variable

Function:Int encrypt (void * objptr, char * outbuffer, lpstr datainbuffer, lpstr keyinbuffer)

 

Input parameters:

Cstring datainbuffer, plaintext to be encrypted

Cstring keyinbuffer encryption key

Requirements: 8-16 characters (actual range: 0-16 characters)

Output parameters:

Char * outbuffer, encrypted ciphertext

Format: (number of characters in plaintext/8 + 1) * 16

2. decryption

I = decrypt (object, original text, ciphertext, key)

I = 1, the encryption is successful; I = 0, the encryption fails; I is an integer variable

Function:Int decrypt (void * objptr, char * outbuffer, lpstr datainbuffer, lpstr keyinbuffer)

Input parameters:

Cstring datainbuffer, ciphertext to be decrypted

No format requirements

Cstring keyinbuffer decryption key

Requirements: 8-16 characters (actual range: 0-16 characters)

Output parameters:

Char * outbuffer, decrypted plaintext

Format: original string

3. Object

Void * createdes_dll ()

First, implement a Java code to declare native functions and parameters.

Package com. daxiannetwork. util;

Public class jdes {

/**
* Kaede
*/
Static {

System. loadlibrary ("com_daxiannetwork_util_jdes ");

}

Public native string decrypt (string cryptograph, string key );
// Declare the native function decrypt for decryption
Public native string encrypt (string proclaim, string key );
// Declared native function encrypt for encryption

Public static void main (string [] ARGs ){

Jdes = new jdes ();

String A = jdes. Encrypt ("123", "1 ")
// 1 is the key
String B = jdes. Encrypt (a, "1 ");

System. Out. println (a + ":" + B );

}
}

Then, compile (not described here) to generate jdes. Class.
Use javah.exe to generate the C header file.
This must be said, because our class is included in COM. daxiannetwork. util.

Javah-JNI-d:/Eclipse/workspace/GUI/COM/daxiannetwork/util com. daxiannetworkt. util. jdes

In this directory, D:/Eclipse/workspace/GUI/COM/daxiannetwork/util has a com_daxiannetwork_util_jdes.h
This is the header file.

# Include "JNI. H"
# Ifndef _ included_com_daxiannetwork_util_jdes
# DEFINE _ included_com_daxiannetwork_util_jdes
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: com_daxiannetwork_util_jdes
* Method: decrypt
* Signature: (ljava/lang/string;) ljava/lang/string;
*/
Jniexport jstring jnicall java_com_daxiannetwork_util_jdes_decrypt
(Jnienv *, jobject, jstring, jstring );

/*
* Class: com_daxiannetwork_util_jdes
* Method: Encrypt
* Signature: (ljava/lang/string;) ljava/lang/string;
*/
Jniexport jstring jnicall java_com_daxiannetwork_util_jdes_encrypt
(Jnienv *, jobject, jstring, jstring );

# Ifdef _ cplusplus
}
# Endif
# Endif

Look, what is the difference with the normal c header file,
Jniexport jstring jnicall java_com_daxiannetwork_util_jdes_encrypt (jnienv *, jobject, jstring, jstring );
Jniexport and jnicall are macros and will be expanded as platform-specific commands. Jnienv, jobject, jstring, and jstring
The data type defined by JNI.
Java_com_daxiannetwork_util_jdes_encrypt: This is the function name. All native functions are identified by Java, followed by the package name. Haha, so that the C ++ code must implement this method, but the original DLL function is not like this,
So I use Java-> yourdll-> otherdll.

Now let's implement our own DLL!
Use VC ++ to create a com_daxiannetwork_util_jdes MFC project to dynamically connect to the Library (because the existing DLL is implemented using MFC ), then, replace % java_home %/include with % java_home %/include/Win32. add the H file to the project,
Next, modify com_daxiannetwork_util_jdes.h generated by the VC ++ project, and open and copy the. h file generated by javah in notepad.
Paste all the content to the front of com_daxiannetwork_util_jdes.h generated by the VC ++ project.
To implement this local function, open com_daxiannetwork_util_jdes.cpp and add the following code.

# Include "com_daxiannetwork_util_jdes.h"

// Call the DLL header file
# Include "des_dll_ I .h"

Jniexport jstring jnicall java_com_daxiannetwork_util_jdes_decrypt
(Jnienv * ENV, jobject OBJ, jstring cryptograph, jstring key ){

Const char * t_cryptograph = env-> getstringutfchars (cryptograph, 0 );

Const char * t_key = env-> getstringutfchars (Key, 0 );

Void * objptr = createdes_dll ();

Char * outputstr = new char [1000];

Char mingwen [1000];

Int result = decrypt (objptr, outputstr, t_cryptograph, t_key );

Strcpy (mingwen, outputstr );

// Tell the VM that the local code is not authorized to use UTF

Env-> releasestringutfchars (cryptograph, t_cryptograph );

Env-> releasestringutfchars (Key, t_key );

Delete [] outputstr;

Return env-> newstringutf (mingwen );
}

// Encryption

Jniexport jstring jnicall java_com_daxiannetwork_util_jdes_encrypt
(Jnienv * ENV, jobject OBJ, jstring proclaim, jstring key ){

Const char * t_proclaim = env-> getstringutfchars (proclaim, 0 );

Const char * t_key = env-> getstringutfchars (Key, 0 );

Void * objptr = createdes_dll (); // call a method in other DLL.

Char * outputstr = new char [1000];

Char miwen [2, 1000];

Int result = encrypt (objptr, outputstr, t_proclaim, t_key); // call a method in other DLL.

Strcpy (miwen, outputstr );

Env-> releasestringutfchars (proclaim, t_proclaim );

Env-> releasestringutfchars (Key, t_key );

Delete [] outputstr;

Return env-> newstringutf (miwen );
}

Compile, generate com_daxiannetwork_util_jdes.dll, and then register the two DLL into the path.
Run the program.

This section describes how to call the 3rd-party DLL, but it is not suitable for scholars. In fact, they are all the same, that is, communication between jdes. Class and com_daxiannetwork_util_jdes.dll.

To remove the purple part, only jdes. Class communicates with com_daxiannetwork_util_jdes.dll.

For more details about JNI, see:
Http://java.sun.com/j2se/1.3/docs/guide/jni/spec/jniTOC.doc.html

JNI calls an existing DLL, which is not well written. Sorry !!

If you have any questions, you just need to ask. I often come here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.