Java calls a C + + write DLL

Source: Internet
Author: User

Source: Java calls the DLL written by C + +

The Java language itself is cross-platform, and using Java to develop a foreground interface can be faster and cross-platform if the technology for invoking DLLs is easy to use.

When Java calls a DLL library that is written in C + +, due to the different basic data types and the possible differences in the use of byte sequences, there may be a problem in the parameter passing process, and the corresponding conversion might be required in the DLL.

There are typically three scenarios for invoking DLL dynamic-link libraries using Java: JNI, Jawin, Jacob. Its JNI (Java Native Interface) is a method of invoking a locally compiled library of functions provided by the Java language itself, which is inherently cross-platform and can invoke different local libraries on different machines. Both Jawin and Jacob are sourceforge.net open source projects that rely on the Windows-based implementation of JNI technology, making it easier to use COM and DLLs under the Windows platform.

Let's take a look at the use of JNI to implement a call between Java and C + +.

First, JNI Introduction

JNI is the abbreviation for Java Native interface. Beginning with Java 1.1, the Java Native Interface (JNI) standard became part of the Java platform, allowing Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other languages, as long as the calling convention is supported.

Sun Related documents: http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html

Second, Simple Steps

The first step:

Write the Java class, using the class to declare the function service provided by the DLL, in which the Java method is declared as native, and its method signature can be customized without implementing the function body. An example is as follows: Dlltest.java

public class Dlltest

{

Static

{

System.loadlibrary ("chat");

}

public static native int connect (int i);

public static native string GetString (string src);

public static void Main (string[] args)

{

Dlltest dlltest = new Dlltest ();

System.out.println (Dlltest.connect (5));

System.out.println (dlltest.getstring ("Java send"));

}

}

Where chat is the name of the dynamic library, its extension name can not be written out, whether it is a DLL or so, by the system's own judgment. Here, two interface functions are defined, one is the Connect function with the Enter and return values of the int type, and the GetString function that has the arguments and return values of the string object. None of them need concrete implementations.

 

Step Two:

Use the Javah tool to generate the corresponding. h header file for the Java class. First, compile the Dlltest.class file with Javac Dlltest.java, then compile a dlltest file with Javah DllTest.h. This DllTest.h is the header file in the C/s + + dynamic library, which needs to be implemented. DllTest.h Header File Contents:

/* Don't EDIT this file-it are machine generated */

#include <jni.h>

/* Header for class Dlltest */

#ifndef _included_dlltest

#define _included_dlltest

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:dlltest

* Method:connect

* Signature: (i) I

*/

Jniexport Jint Jnicall Java_dlltest_connect

(JNIEnv *, Jclass, jint);

/*

* Class:dlltest

* method:getstring

* Signature: (ljava/lang/string;) ljava/lang/string;

*/

Jniexport jstring Jnicall java_dlltest_getstring

(JNIEnv *, Jclass, jstring);

#ifdef __cplusplus

}

#endif

#endif

Here Jniexport and jnicall are all JNI keywords, indicating that this function is to be called by JNI. The Jint and jstring are intermediate types between data types in Java and data types in C + +, which are mediated by JNI. Jint can be used directly as an int, but the jstring cannot be equated with char * and requires a certain conversion. The name of the function is Java_ plus the package path of the Java program and the name of the Add function. parameter, we only need to care about the parameters that exist in the Java program, as for jnienv* and jclass we generally do not need to touch it.

 

Step Three:

Write the C + + code to implement the functions declared in the. h header file, which contains the jni.h header file.

int i = 2;

const char *TESTSTR = "C + + return";

Jniexport Jint Jnicall Java_dlltest_connect

(JNIEnv *, Jclass, Jint J)

{

i = i + j;

return i;

}

Jniexport jstring Jnicall java_dlltest_getstring

(JNIENV * env, Jclass, jstring policy)

{

jstring conversion to char * (UTF-8)

const char * s1 = Env->getstringutfchars (policy, false);

printf ("%s/n", S1);

char * (UTF-8) converted to jstring

Jstring js = Env->newstringutf (TESTSTR);

Return JS;

}

The build Char.dll dynamic library is then compiled using the C + + compiler tool. Put this dynamic library in the same folder as the Dlltest.class in the second step.

Fourth Step:

Using Java Dlltest to execute the call, you can see the output:

7

Java Send

C + + return

Java calls the DLL (go) written by C + +

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.