Call the DLL generated by C/C ++ in Java

Source: Internet
Author: User
Call the DLL generated by C/C ++ in Java

I. generate C header files

1. Edit main. Java
 
  1. Public class main
  2. {
  3. Public native static int getstrnum (byte STR [], int strlen );
  4. }

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:

 

  1. # Include "Main. H"
  2. # Include <string. h>
  3. Jniexport jint jnicall java_main_getstrnum (jnienv * ENV, jclass CLs, jbytearray STR, jint strlen)
  4. {
  5. Jbyte * buffer = (* env). getbytearrayelements (STR, 0 );
  6. Buffer [strlen] = ''; // the string length parameter needs to be passed in Because Java automatically allocates space.
  7. Jint Len = strlen (char *) buffer );
  8. Return Len;
  9. }

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:
  

  1. Public class main
  2. {
  3. Static
  4. {
  5. System. loadlibrary ('makedll ');
  6. }
  7. Public native static int getstrnum (byte STR [], int strlen );
  8. Public static void main (string [] ARGs)
  9. {
  10. Main instance = new main ();
  11. String STR = new string ('hahaha ');
  12. Int num = instance. getstrnum (Str. getbytes (), str. Length ());
  13. System. Out. println (Num );
  14. }
  15. }

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 ".

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.