Call the DLL generated by C/C ++ in Java
I. generate C header files
1. Edit main. Java
- Public class main
- {
- Public native static int getstrnum (byte STR [], int strlen );
- }
2. Generate the header file
Press win + R to open the "run" window, and enter "cmd" to open the DOS Console window. Go to the directory where main. Java is located and enter:
Javac main. Java
Javah main
After the two commands are completed, the main. h file is generated.
Ii. generate DLL
1. Create an empty project
Create a project in vs: Win32 console application, named "makedll", and select an empty project. After the project is created, modify the project properties:
(1) Change "configuration type" to "dynamic library (. dll)" in general )";
(2) In C/C ++ General, add "Additional include directories" to two items:
"C: Program filesjavajdk1.5.0 _ 06include"
"C: Program filesjavajdk1.5.0 _ 061_dewin32"
Set it based on the Java path you have installed. This is because Java JNI. h and other files are used during C/C ++ compilation.
2. Add the generated main. H to the project.
3. Create a new file main. cpp. The Code is as follows:
- # Include "Main. H"
- # Include <string. h>
- Jniexport jint jnicall java_main_getstrnum (jnienv * ENV, jclass CLs, jbytearray STR, jint strlen)
- {
- Jbyte * buffer = (* env). getbytearrayelements (STR, 0 );
- Buffer [strlen] = ''; // the string length parameter needs to be passed in Because Java automatically allocates space.
- Jint Len = strlen (char *) buffer );
- Return Len;
- }
4. Press F7 to compile the link and generate the makedll. dll file.
Iii. Calling DLL in Java
Use the DLL file generated above in Java:
1. Copy makedll. DLL to the same directory as the Java File Using DLL.
2. source code of the Java file:
- Public class main
- {
- Static
- {
- System. loadlibrary ('makedll ');
- }
- Public native static int getstrnum (byte STR [], int strlen );
- Public static void main (string [] ARGs)
- {
- Main instance = new main ();
- String STR = new string ('hahaha ');
- Int num = instance. getstrnum (Str. getbytes (), str. Length ());
- System. Out. println (Num );
- }
- }
Summary:
In addition, the passing process of parameters of Java and DLL Functions, examination, prompts like int type, can be obtained directly from the JNI function parameter list; and the array element should be obtained through jnienv:
Jbyte * buffer = (* env). getbytearrayelements (STR, 0); // STR is the jbytearray variable in the parameter list
Similarly, you can obtain the elements of char.
You can use the return statement to return a function. You can also set a variable pointer in the parameter list to return the result:
(* Env). setbytearrayregion (bytearray, 0,128, bytearr );
Bytearray is the jbytearray variable in the parameter list. It is used as a pointer to return the information to be returned. bytearr is of the jbyte * type, and the value in it is the content to be returned.
Figure 1 interaction between C ++ and Java
For other information, refer to Google or Baidu for "JNI" or "Java Call DLL ".