A Method for Java to call C ++ DLL

Source: Internet
Author: User

Http://topic.csdn.net/t/20020917/14/1030420.html

 

Thanks for your support. I have found many questions about JNI on the IBM Chinese and Sun official websites over the past few days. Article Hope

If you have any questions, you can find the relevant information.

1) Implement the Java local method in Windows
Http://www-900.ibm.com/developerWorks/cn/java/jnimthds/index.shtml
2) Java Native Interface: programmer's guide and Specification
Http://java.sun.com/docs/books/jni

The above article introduces JNI in great detail. If you are interested in JNI, you can check it out.

I have provided a successful example of my question and I will share it with you.
There is a mydll. dll library, interface trim (lpstr lpstring), the function is to remove spaces in lpstring. Original file code

As follows:
# Include <string. h>
# Include <windows. h>

# Ifdef _ cplusplus
Extern "C "{
# Endif
_ Declspec (dllexport) void trim (lpstr lpstring)
{
Int ilength = strlen (lpstring );
Int icount = 0;
Char * lpchar = lpstring;
Int I;
For (I = 0; I <ilength; I ++)
{
If (* lpchar! = '')
{
Lpstring [icount] = * lpchar;
Icount ++;
}
Lpchar ++;
}
Lpstring [icount ++] = 0;
}

# Ifdef _ cplusplus
}
# Endif

(Use vc6 Cl to compile this file to generate a dynamic link library, Cl-LD mydll. C-femydll. dll)

Assume that we cannot obtain the original Code Only DLL and Lib files, when called using Java through JNI

Because we do not have the permission to modify the original DLL code, we need to use C/C ++ to make another intermediary library dll2. Java calls this

The intermediary library, which then calls the original dll library, indirectly calls the Java local method.
The following is an example of my environment: win2000server, jdk1.3, vc6.0

Compile a Java Program , Java2dll. Java, the Code is as follows:
Public class java2dll
{




// Declare the local method to be used. ctrim is implemented by the intermediary Library (java2dll. dll), and it calls the T of the mydll. dll library.

Rim (...) Function
Private native string ctrim (string Str );
Static
{
System. loadlibrary ("java2dll"); // load the local method (java2dll is dynamic

Link Library file name)
}

// A self-defined Java method. Calling the local method ctrim () is a bit redundant. In fact, you can directly access

Ask the local method ctrim ()
Public String javatrim (string Str)
{
Return ctrim (STR );
}

// Demo result
Public static void main (string [] ARGs)
{
Java2dll Ss = new java2dll ();
String str1 = "I love you .";
String str2 = ss. javatrim (str1 );
System. Out. println ("before calling C dll :");
System. Out. println ("str1 =" + str1 );
System. Out. println ("after calling C dll :");
System. Out. println ("str2 =" + str2 );
}
}

Compile the file to generate the java2dll. Class file:
Javac java2dll. Java
Generate the C header file java2dll. h:
Javah-JNI java2dll

Use C to compile the intermediary library java2dll. c
# Include <stdio. h>
# Include "java2dll. H"

Jniexport jstring jnicall java_java2dll_ctrim
(Jnienv * ENV, jobject OBJ, jstring Str)
{
Const char * strtemp;
Strtemp = (* env)-> getstringutfchars (ENV, STR, null );
// When an error occurs
If (strtemp = NULL)
{
Return NULL;
}

// Call the trim () method in the mydll. dll library to remove spaces in strtemp.
Trim (strtemp );
Return (* env)-> newstringutf (ENV, strtemp );

(* Env)-> releasestringutfchars (ENV, STR, strtemp );
}
Note: If you use C ++ to write the CPP file, replace "(* env)" in the above Code with "env"

Compile and generate the java2ddl. dll file.
Compile with Cl of vc6.
CL-ID: \ jdk13 \ include-ID: \ jdk13 \ include \ Win32-LD java2dll. c mydll. Lib-fejava2dll. dll
-:-ID: \ jdk13 \ include specifies the position of the header file. h required by JNI, and D: \ jdk13 indicates the JDK installation directory.
-LD indicates that a dynamic link library is to be generated.
Mydll. lib or-Link

Mydll. Lib indicates that you want to link mydll. dll; otherwise, the trim (...) method cannot be found in the intermediary library java2dll.
You can use Cl /? Get help.

After compilation is successful, you can get the intermediary library java2dll, run it, and check the result.
Java java2dll

Okay. You can try it yourself.

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.