Sometimes, a project is not composed of a single language, so avoiding the cooperative relationship between language and language. For example: At this point 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, such as a DLL, taking into account the efficiency, you use this DLL more better. At this time you have to use JNI (Java Native Interface) -Java native interface. This way we start from a Java called an existing DLL example. The DES.DLL call interface is as follows:
1, encryption i = encrypt (object, ciphertext, clear text, key) i = 1 successfully encrypted; i = 0 failed to encrypt; i is an integer variable function: int Encrypt (void * Objptr, char * Outbuffer, lpstr DataINBuffer, LPSTR Keyinbuffer input parameters: CSTRING DATAINBUFER, to encrypt the plain text CString KeyINBuffer encryption key requirement: 8-16 characters, (actual range 0-16 characters) Output parameters: char * OUTBUFFER, encrypted Ciphertext format: (明文 字号 号 / 8 1) * 16 2, decrypt i = decrypt (object, original, ciphertext, key) i = 1 is successful; i = 0 When encryption failed; i is an integer variable function: int Decrypt (Void * Objptr, Char * Outbuffer, LPSTR DATAINBUFFER, LPSTR Keyinbuffer) Enter parameters: CSTRING DATAINBUFER Output parameters: char * OUTBUFFER, decrypted plaintext format: Original string 3, object void * createdes_dll ()
First, realize a Java code, declare the original function 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); native function Decrypt // declaration, Used to decrypt PUBLIC NATIVE STRING Encrypt (String Proclaim, String Key); // Declared native function Encrypt for encryption public static void main (string [] args) {jdes 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), generate JDES.CLASS.
Next, generate C's header file with javah.exe.
This must be said, because our class is included in com.daxiannetwork.util.
Javah -jni -d D: / Eclipse / Workspace / GUI / COM / DAXIANNETWORKT.UTIL.DAXINETWORKT.UTIL.JDES
In this directory D: / Eclipse / Workspace / GUI / COM / DAXIANNETWORK / UTIL has a com_daxiannetwork_util_jdes.h
This is the header file we have to get.
#include "jni.h" #ifndef _Included_com_daxiannetwork_util_JDES # define _Included_com_daxiannetwork_util_JDES # ifdef __cplusplusextern "C" {# endif / * * Class: com_daxiannetwork_util_JDES * Method: Decrypt * Signature: (Ljava / lang / String; 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;) 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_DAXINETWORK_UTIL_JDES_ENCRYPT (JNIENV *, JOBJECT, JSTRING, JSTRING); JNIEXPORT, JNILL This is a macro, which will be expanded as platform proprietary instructions. JNienv, Jobject, JString, JString are all
JNI defined data type.
Java_com_daxiannetwork_util_jdes_encrypt This is a function name, all native functions are identified by Java, followed by the package name method name. Oh, this makes the C code must implement such a method, but the original DLL function is not like this.
So I use java-> yourdll-> Otherdll.
Now let's implement your own DLL!
Create a COM_DAXIANNETWORK_UTIL_JDES MFC dynamic connection library with VC (Since the existing DLL is implemented with MFC), then the% java_home / include with% java_home / include / win32 is added to the project,
Next, modify the COM_DAXINETWORK_UTIL_JDES.H generated by the VC project, and the Javah is generated. H file opens with Notepad, copy
All content, pasted into the COM_DAXINETWORK_UTIL_JDES.H generated by the VC project, the top.
Implement this local function, open COM_DAXINETWORK_UTIL_JDES.CPP, add the following code, as follows.
#include "com_daxiannetwork_util_JDES.h" DLL header file #include // will be called "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 VM native code is not entitled 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 = creteds_dll (); // call One method in Other DLL. Char * OutputStr = New Char [1000]; int result = encrypt (Objptr, OutputStr, T_ProcLaim, T_Key); // Call a method in the Other DLL. STRCPY (MIWEN, OUTPUTSTR); ENV-> ReleaseStringutfchars (Proclaim, T_ProcLaim); Env-> ReleaseStringutfchars (key, t_key); delete [] OutputStr; Return ENV-> Newstringutf (miwen);}
Compile, generate COM_DAXINETWORK_UTIL_JDES.DLL, then register these two DLLs into the path. Run the program. Oh, this introduces the DLL that calls the third party, but it is not suitable for the scholar. Actually, JDES.CLASS communicates with com_daxiannetwork_util_jdes.dll. Remove the purple part is only jdes.class communicated with com_daxiannetwork_util_jdes.dll, huh, it is straight. Oh, please see about JNI more details: http://java.sun.com/j2se/1.3/docs/guide/jni/Spec/jnitoc.doc.htmljni Call existing DLL, huh, huh, writing is not good. Forgive me! ! If you have any questions, you will ask here, I often come.