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
- # Ifndef mingw_dll_h __
- # Define mingw_dll_h __
- IntAdd (IntA,IntB );
- # 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
- # Include <stdio. h>
- # Include "test. H"
- IntAdd (IntA,IntB ){
- Printf ("/N ");
- Printf ("DLL function add () called/N ");
- ReturnA + B;
- }
#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
- 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
- ImportOrg. xvolks. jnative. jnative;
- ImportOrg. xvolks. jnative. type;
- ImportOrg. xvolks. jnative. Exceptions. nativeexception;
- Public ClassTest {
- Public StaticIntTestjnative (IntA,IntB)ThrowsNativeexception, illegalaccessexception {
- Jnative n =Null;
- Try{
- N =NewJnative ("Zzz. dll", "add ");
- N. setretval (type. INT );
- N. setparameter (0, );
- N. setparameter (1, B );
- N. Invoke ();
- System. Out. println ("Return:" + N. getretval ());
- ReturnInteger. parseint (N. getretval ());
- }Finally{
- If(N! =Null)
- N. Dispose ();
- }
- }
- Public Static VoidMain (string [] ARGs)ThrowsNativeexception, illegalaccessexception {
- Testjnative (1, 4 );
- }
- }
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!