Java invoke C-language DLL file method

Source: Internet
Author: User
      sometimes we have to call the C language DLL file in Java, we will use an example to introduce Java invoke C language DLL file implementation method   first,   Generates a header file for C   1.  edit main.java  public class main  {  public native static&n Bsp;int getstrnum (Byte str[], int strlen); }  2.  Generation header file   Press win +  r Open the "Run" window, enter "CMD", open the DOS console window. Enter the directory above Main.java, enter:  javac main.java  javah main  Two command will be generated Main.h file   II,   build dll  1.  new Air Engineering   new project in VS: Win32 console application, named "Makedll", select the Empty project. Modify engineering Properties after new:  (1)  general, change "Configuration type" to "dynamic library  (. dll)";  (2) In  c/c++ 's general, add "additional include directories" to two:  "c:program filesjavajdk1.5.0_ 06include " " C:program filesjavajdk1.5.0_06includewin32   is set according to the Java path you have installed, because it is used during the C + + compilation process Java jni.h files   2.  Add the Main.h built above to project   3.&NBSPCreate a new file Main.cpp, code 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] =  '; //  due to the automatic allocation of space in Java, the string length parameters need to be passed in   Jint len = strlen ((char*) buffer);  return len; }  4.  compile links by F7 to generate MAK EDLL.dll files   III   Java call dll  use the DLL file generated above in Java:  1.  to copy MakeDLL.dll to the same directory as the Java file that uses the DLL.   2.  Java File source code:  public class main  {  static  {  System.loadli Brary (' Makedll '); }  public native static int getstrnum (byte str[], int    strlen);  Public static void main (String[] args)   { Main instance = new main ();  string str = new string (' haha ');   Int num = instance.getstrnum (Str.getbytes (),  str.length ());  System.out.println ( num); }  [Page]}  Summary:  In addition, the parameter passing process of Java and DLL functions, like the type int, can be obtained directly from the JNI function argument list, while the array elements are obtained by jnienv: &N   Bsp jbyte *buffer =  (*env). Getbytearrayelements (str, 0);  // str is the element in the parameter list Jbytearray variable   can get char[]. When the   function returns, it can be returned directly with the return statement. You can also set the variable pointer in the argument list to return to:  (*env). Setbytearrayregion (Bytearray, 0, 128, bytearr);  ByteArray is a jbytearray variable in the argument list. Used as a pointer to return the information that needs to be returned; The Bytearr is the jbyte* type, where the value is the content to be returned.   Other information can be accessed from Google or Baidu by looking for "JNI" or "Java calling DLL".
Related Article

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.