Source: Java calls DLL verbose process through JNI
Recent projects have such a requirement, in the existing CS software to add a link, the current logged-in user's username is encrypted and placed in the URL address, in the BS login method through decryption judgment, if the legitimate user does not need to login directly into the platform, CS software to provide a cryptographic decrypted DLL file, we need to decrypt the DLL in action, then it involves the Java call DLL problem.
First I chose the Jni method (because there are two other ways Jawin, Jacob is not), the general process is as follows:
1. Write a Java class that declares the name of the called Library and the function to be used in the class (note: a local declaration of the method is required and the keyword is native. And only need to be declared, without the need for a specific implementation)
Packagecom; Public classjavacall{Static{system.loadlibrary ("Htgsjencrypt"); } Public native StaticString decodestring (Char[] szsrc); Public native StaticString encodestring (Char[] szsrc); Private Static voidPrintchararray (Char[] content) {String temp=NewString (content); SYSTEM.OUT.PRINTLN (temp); } Public Static voidMain (string[] args) {String s= "123"; Char[] src=New Char[100]; SRC=S.tochararray (); String encode=""; Printchararray (SRC); Encode=Javacall. Encodestring (SRC); System.out.println ("Encode=" +encode); String Decode=""; SRC=Encode.tochararray (); Decode=Javacall. Decodestring (SRC); System.out.println ("Decode=" +decode); }}
This place needs to mention that the new class should not be built in the default package, in the future after the project is packaged, in the referenced project can not find the default package in the class (perhaps I write the wrong, but write in the default package does cause unnecessary trouble)
2, for the above compiled class file by using the Javah command to generate the header file javacall.h, this file needs to be called C + + program to generate the required library files
/*Do not EDIT this file-it are machine generated*/#include"Jni.h"/*Header for Class Com_javacall*/#ifndef _included_com_javacall#define _included_com_javacall#ifdef __cplusplusexternC{#endif/** Class:com_javacall * method:decodestring * Signature: ([C) ljava/lang/string;*/jniexport jstring jnicall java_com_javacall_decodestring (jnienv*, Jclass, Jchararray);/** Class:com_javacall * method:encodestring * Signature: ([C) ljava/lang/string;*/jniexport jstring jnicall java_com_javacall_encodestring (jnienv*, Jclass, Jchararray); #ifdef __cplusplus} #endif #endif
One thing to mention here is that the default generated header file is written in
#include <jni.h>
In C + + CALSS reference when the compilation error cannot find jni.h, you can go to the JDK installation package in the Include folder copy jni.h, Jni_md.h, jawt_md.h three files to the program directory, then compile may also reported can not find jni.h error, you can add # Include <jni.h> change to # include "Jni.h" because the former is a notation for referencing the system header file
3, in the VC new library file Htgsjencrypt, in the new class file implementation of the Java header file declared in the two encryption decryption method, Because the third party does not provide the. lib file, and there is no. h file, the DLL can only be called dynamically using the link library, as follows:
#include"gsjencrypt.h"#include"com_javacall.h"#include"windows.h"#include<iostream>////////////////////////////////////////////////////////////////////////construction/destruction//////////////////////////////////////////////////////////////////////Gsjencrypt::gsjencrypt () {}gsjencrypt::~Gsjencrypt () {}typedefint(WINAPI *fdecodestring) (Charszsrc[ -],Charszdest[ -]); typedefint(WINAPI *fencodestring) (Charszsrc[ -],Charszdest[ -]); Jniexport jstring jnicall java_com_javacall_encodestring (jnienv*env, Jclass jobject, Jchararray src) {hinstance hdll; hDLL=loadlibrary ("Gsjencrypt.dll");//load the dynamic link library Gsjencrypt.dll file; if(hdll==NULL)return 0; Fencodestring encodestring= (fencodestring) GetProcAddress (hDLL,"encodestring"); Jsize size= (env)getarraylength (SRC); Jchar* Arraybody = (env)->getchararrayelements (SRC,0); //char * csrc= (char *) arraybody; Charcsrctemp[ -]=""; intk=0; while(size!=0) {Csrctemp[k]=*Arraybody; *arraybody++; Size--; K++; } Charcdesttemp[ -]=""; Encodestring (csrctemp,cdesttemp); (env)->releasechararrayelements (Src,arraybody,0); return(ENV)Newstringutf (cdesttemp);} Jniexport jstring jnicall java_com_javacall_decodestring (jnienv*env, Jclass jobject, Jchararray src) {hinstance hdll; hDLL=loadlibrary ("Gsjencrypt.dll");//load the dynamic link library Gsjencrypt.dll file; if(hdll==NULL)return 0; Fdecodestring decodestring= (fdecodestring) GetProcAddress (hDLL,"decodestring"); Jsize size= (env)getarraylength (SRC); Jchar* Arraybody = (env)->getchararrayelements (SRC,0); Char* Csrc= (Char*) Arraybody; Charcsrctemp[ -]=""; intk=0; while(size!=0) {Csrctemp[k]=*Arraybody; *arraybody++; Size--; K++; } //arraybody= (env)->getchararrayelements (dest,0); //char * cdest= (char *) arraybody; Charcdesttemp[ -]=""; Decodestring (csrctemp,cdesttemp); (env)->releasechararrayelements (Src,arraybody,0); return(ENV)Newstringutf (cdesttemp);}
It's important to note that
int (WINAPI *fdecodestring) (char szsrc[-char szdest[ (WINAPI *fencodestring) ( Char szsrc[-char szdest[
The above two methods are provided in the original DLL to the outside call of the function interface, declare must remember to add WINAPI, otherwise the call always error.
4. Finally, the third-party provided Gsjencrypt.dll and the newly generated htgsjencrypt.dll are copied to the Java.library.path (JDK or JRE bin file), and then the Java program that is written first is packaged to be called by another project.
The most difficult problem to record is that you don't know how to return the decrypted string to Java in C + +, because you always want to associate the concept of pointers with a byte array or a char array in Java, never succeed, and finally try to use NEWSTRINGUTF to solve the problem, eh, C + + has been useless for six years.
Java calls DLL verbose process via JNI (GO)