Compile the DLL using GCC and call the DLL using Java.

Source: Internet
Author: User

We use examples to learn how to compile DLL files using gcc. The following example shows the process. There are three files in total: Hello. c. dll. H and DLL. c. hello. the content of the C file is as follows # include <stdio. h> # include/"DLL. h/"int main ()

{Hello (); Return 0;} among them, the hello () function is a function provided by the dynamic Connection Library.

The DLL. h file content is as follows # ifdef build_dll/* DLL Export */# define export _ declspec (dllexport)

# Else/* EXE import */# define export _ declspec (dllimport)

# Endif export void Hello (void );

The DLL. c file content is as follows # include/"DLL. h/" Export void Hello (void)

{Printf (/"Hello // n /");}


The content of the three files is very simple and does not need to be explained.

Compile the Connection Program

1. Compile hello. c gcc-C hello. C.

2. Compile DLL. c gcc-C-dbuild_dll. C. Use-dbuild_dll to set the macro build_dll.

3. Create a DLL gcc-shared-O message. DLL. o-wl, -- Out-implib, libmessage. a. The shared parameter is used to create a shared library. In Windows, DLL-WL is used to wait for the next information to connect. The out-implib parameter is used by the Connection Program lD, used to create the import library required to connect to the DLL.

4. Create the executable file gcc-O hello.exe hello. o-l. /-lmessage-L specifies the connection library path-lmessage (or-l message) specifies the DLL import library. The compilation connection is complete and the program C: //> hello!

 

 

 

 

 

 

 

The following is an example of calling a DLL using Java.

 

Recently I used Java to Call DLL and found the jnative open source on the Internet.
How to use it?
Write a DLL experiment by yourself.
Therefore, the C/C ++ compiler is recommended for mingw.
Write a head file first:
Test. h:

 

C code
  1. # Ifndef mingw_dll_h __
  2. # Define mingw_dll_h __
  3. IntAdd (IntA,IntB );
  4. # Endif
#ifndef MINGW_DLL_H__#define MINGW_DLL_H__int add(int a,int b);#endif

 

 

Then write the c file:
Test. C:

C code
  1. # Include <stdio. h>
  2. # Include "test. H"
  3. IntAdd (IntA,IntB ){
  4. Printf ("/N ");
  5. Printf ("DLL function add () called/N ");
  6. ReturnA + B;
  7. }
#include <stdio.h>#include "test.h"int add(int a,int b){  printf("/n");  printf("dll function add() called/n");  return a+b;}

 

 

Use mingw to compile C into a DLL. The command is as follows:

C code
  1. Gcc-wall-shared test. C-O test. dll
gcc -Wall -shared test.c -o test.dll

 

 

Put test. dll under system32,
Finally, it is time to write our Java code:
Test. Java:

Java code
  1. ImportOrg. xvolks. jnative. jnative;
  2. ImportOrg. xvolks. jnative. type;
  3. ImportOrg. xvolks. jnative. Exceptions. nativeexception;
  4. Public ClassTest {
  5. Public StaticIntTestjnative (IntA,IntB)ThrowsNativeexception, illegalaccessexception {
  6. Jnative n =Null;
  7. Try{
  8. N =NewJnative ("Zzz. dll", "add ");
  9. N. setretval (type. INT );
  10. N. setparameter (0, );
  11. N. setparameter (1, B );
  12. N. Invoke ();
  13. System. Out. println ("Return:" + N. getretval ());
  14. ReturnInteger. parseint (N. getretval ());
  15. }Finally{
  16. If(N! =Null)
  17. N. Dispose ();
  18. }
  19. }
  20. Public Static VoidMain (string [] ARGs)ThrowsNativeexception, illegalaccessexception {
  21. Testjnative (1, 4 );
  22. }
  23. }
Import Org. xvolks. jnative. jnative; import Org. xvolks. jnative. type; import Org. xvolks. jnative. exceptions. nativeexception; public class test {public static int testjnative (int A, int B) throws nativeexception, illegalaccessexception {jnative n = NULL; try {n = new jnative ("Zzz. DLL "," add "); N. setretval (type. INT); N. setparameter (0, a); N. setparameter (1, B); N. invoke (); system. out. println ("Return:" + N. get Retval (); Return integer. parseint (N. getretval ();} finally {If (n! = NULL) n. Dispose () ;}} public static void main (string [] ARGs) throws nativeexception, illegalaccessexception {testjnative (1, 4 );}}

 

 

OK!
Run the following command to try it. The result is as follows:
Return Value: 5.
DLL function add () called

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--------------- >>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>

This is not troublesome, but mingw is difficult, but after all, it has been done. The following is my method:
First to the http://sourceforge.net/project/showfiles.php? Group_id = 2435
Find the first one: Automated mingw installer, download it, and install it. During installation, select all installation options,
It automatically downloads all required files.
At the beginning, I downloaded files one by one according to the method described on the Internet. If I didn't pay attention to this, I always reported fewer files,
Later, I found out that he deleted all the original ones. OK!

Jnative is: http://sourceforge.net/project/showfiles.php? Group_id = 156421, This is not difficult,
Put the DLL it carries under system32 and it will be OK!

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.