These two days to understand the Java call DLL Dynamic library method, there are three kinds: JNI, JNA, jnative. Where the JNA call DLL is the most convenient.
· Jni
· JNA
· Jnative
Java uses JNI to invoke DLL dynamic library calls, the workload is slightly larger, in general, developers will choose JNA or jnative.
Calling a DLL with Jnative requires an additional DLL file to be introduced in addition to the jar package, and JNA only needs to introduce a jar to use.
to invoke DLL dynamic libraries using the JNA framework, proceed as follows :
1. Importing JNA-supported jar packages
Add Jna-4.x.x.jar, Jna-platform-4.x.x.jar to Project BuildPath (maven project can search for JNA dependencies in the central warehouse added to the project)
2. Code Call DLL Dynamic library method
Importcom.sun.jna.Library;Importcom.sun.jna.Native;ImportCom.sun.jna.Platform;Importcom.sun.jna.win32.StdCallLibrary;/*** JNA Framework DLL Dynamic library Read Call example class * @ClassName: Dllcall * @Description: Read method in call DLL dynamic library file *@author: Linwenli * @date: July 18, 2018 morning 10:32:16*/ Public classJnadllcall {/*** DLL Dynamic library call method * @Description: Read call cdecl way exported DLL Dynamic library method *@author: Linwenli * @date: July 18, 2018 morning 10:49:02*/ Public InterfaceClibraryextendsLibrary {//DLL file default path is the project root directory, if the DLL file is stored outside the project, use absolute path. (Here: (Platform.iswindows ()? ") MSVCRT ":" C ") refers to the local dynamic library Msvcrt.dll)Clibrary INSTANCE = (clibrary) native.loadlibrary ((Platform.iswindows ()? "MSVCRT": "C"), Clibrary.class); //declares a method in the DLL that will be called, which can be multiple methods (this example calls the printf () method in the local dynamic library Msvcrt.dll) voidprintf (String format, Object ... args); } Public Static voidMain (string[] args) {CLibrary.INSTANCE.printf ("Hello, world!."); }}
Note: The number of DLL bits to call is the same as the number of JDK bits, otherwise it cannot be called.
Depressed is, in accordance with the official test code above run, the console did not error or print anything, after many tests have no effect I will C:\Windows\System32\msvcrt.dll copied to the computer desktop, and then the code in the DLL path to the absolute path, run successfully.
DLL functions are exported in two ways, in which the StdCall method exports the Dll,jna call code, such as DLL dynamic library call method 1,decl mode exported DLL, calling code such as DLL dynamic library call Method 2
Importcom.sun.jna.Library;Importcom.sun.jna.Native;ImportCom.sun.jna.Platform;Importcom.sun.jna.win32.StdCallLibrary;/*** JNA Framework DLL Dynamic library Read Call Example class * * @ClassName: Dllcall * @Description: Read method in call DLL dynamic library file *@author: Linwenli * @date: July 18, 2018 morning 10:32:16*/ Public classJnadllcall {/*** DLL Dynamic library call Method 1 * * @Description: Read call stdcall way exported DLL Dynamic library method *@author: Linwenli * @date: July 18, 2018 morning 10:37:58*/ Public InterfaceStdcalldllextendsStdcalllibrary {//DLL file default path is the project root directory, if the DLL file is stored outside the project, use absolute pathStdcalldll INSTANCE = (stdcalldll) native.loadlibrary ((Platform.iswindows ()? "MSVCRT": "C"), Stdcalldll.class);//loading a dynamic library file//declares a method in the DLL that will be called (can be multiple methods) voidprintf (String format, Object ... args); } /*** DLL Dynamic library Call Method 2 * * @Description: Read call Decl way exported DLL Dynamic library method *@author: Linwenli * @date: July 18, 2018 morning 10:49:02*/ Public InterfaceClibraryextendsLibrary {//DLL file default path is the project root directory, if the DLL file is stored outside the project, use absolute pathClibrary INSTANCE = (clibrary) native.loadlibrary ((Platform.iswindows ()? "MSVCRT": "C"), Clibrary.class); //declares a method in the DLL that will be called (can be multiple methods) voidprintf (String format, Object ... args); } Public Static voidMain (string[] args) {StdCallDll.INSTANCE.printf ("Hello, world!\n."); CLibrary.INSTANCE.printf ("Hello, world\n."); }}
The above simple record Java through the JNA framework call DLL Dynamic library steps, if you need further understanding can refer to the following blog address: https://www.cnblogs.com/lanxuezaipiao/p/3635556.html
Java calls DLL dynamic library using JNA framework