Source: JNA Call DLL
Introduce to you a new Java framework-jna that accesses native code.
The JNA (Java Native Access) framework is an open-source Java framework that is developed by Sun and is built on the basis of classic JNI.
JNA Project Address: https://jna.dev.java.net/
Very powerful, easy to use, functionally similar to. Net of P/invoke.
The terrible jni we know that using JNI calls. dll/.so Sharing class libraries is very, very cumbersome and painful.
If you have an existing. dll/.so file, if you use the JNI technology call, we first need to write another in C. dll/.so shared library, using the sun-defined data structure instead of the C language, calling functions advertised in the existing dll/so.
Then load the adapter dll/so in Java, and then write the Java native function as the proxy for the function in the DLL.
There are 2 tedious steps to invoke native code in Java.
Therefore, few Java programmers are willing to write Java programs that invoke native functions in the dll/.so library. This also makes the Java language lackluster on the client side. It can be said that JNI is a major weakness of Java!
. NET platform on the powerful p/invoke. NET platform, the powerful P/invoke technology makes our Java programmers very envious. Using P/invoke technology, you only need to write one. NET function, plus a declared callout, you can call the functions in the DLL directly.
You do not need to use the C language to write DLLs to fit.
As p/invoke as the JNA now, no longer need to envy. NET's p/invoke mechanism. JNA reduced the call to the Dll/.so shared library to the same level as P/invoke.
With JNA, you do not need to write an adapter. dll/.so, just write an interface and some code in Java, and as a proxy for the. dll/.so, you can call dll/so in a Java program.
JNA Quick Start
Now let's go straight into the JNA world.
You just need to download a jar package and you can easily invoke the C function in the dynamic-link library using the powerful features of JNA.
1, download Jna.jar.
Here in Http://cloud.github.com/downloads/twall/jna/jna.jar
2, now you are ready to use JNA.
In order to facilitate you to refer to Jna Java class Library, I produced the "JNA3.09API Reference manual", is in CHM format. You can download the http://download.csdn.net/source/900438 here
JNA Example
Example 1 Now let's run a JNA program and feel the power of it.
1, introduce the Jna.jar package in the Java project.
2, create a class:
Importcom.sun.jna.Library;Importcom.sun.jna.Native;ImportCom.sun.jna.Platform;/**Simple example of native library declaration and usage.*/ Public classHelloWorld { Public InterfaceClibraryextendsLibrary {clibrary INSTANCE=(clibrary) native.loadlibrary (platform.iswindows ()? "MSVCRT": "C"), Clibrary.class); voidprintf (String format, Object ... args); } Public Static voidMain (string[] args) {CLibrary.INSTANCE.printf ("Hello, world\n."); for(intI=0;i < args.length;i++) {CLibrary.INSTANCE.printf ("Argument%d:%s\n", I, args[i]); } }}
3, execute, you can see the console print out the
Hello, World
Note, however, that this program actually prints the above characters using the printf function in the C run-time library of Msvcrt.dll.
Look, how simple, no need to write a line of C code, you can directly in Java to call the external dynamic link library functions!
Example 2
The above example uses the dynamic link library that comes with the operating system, and now we'll try to write a dynamic link library ourselves.
1, select the C + + language in VS and choose Create a WIN32 program. Select the DLL type.
2, the published C function is:
#define Mylibapi extern "C" void say (wchar_t* pValue);
The implementation of this function is:
void Say (wchar_t* pValue) { Std::wcout.imbue (Std::locale ("CHS" ) )); Std::wcout<<l" God says:"<<pValue<<Std::endl;}
It needs to pass in a Unicode encoded character array. Then print a Chinese character on the console.
3, build the DLL. Then copy the generated DLL files to the Eclipse project and place them under the project.
4, write the following code in eclipse:
Importcom.sun.jna.Library;Importcom.sun.jna.Native;Importcom.sun.jna.WString;/*** @authorShen Dongliang Edward Shen [email protected]* 2008-11-23 pm 05:07:14*testdll1.dll*/ Public classTestdll1service { Public InterfaceTestDll1extendsLibrary {/*** The current path is under the project, not the bin output directory. */TestDll1 INSTANCE= (TESTDLL1) native.loadlibrary ("TestDll1", TestDll1.class); Public voidsay (WString value); } /** * */ PublicTestdll1service () {//TODO auto-generated Constructor stub } /** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubTestDll1.INSTANCE.say (NewWString ("Hello world!")); System.out.println ("Hheeh me, we can't be evil."); } }
5, execute this Java class. You can see the following output under the console:
God says, Hello world!.
Hheeh me, we can't be evil.
6, the above line is the C language using C + + std::wcout output.
The following line is output from the Java language.
DLL Location:
Mode one: Copy all the dynamic libraries to the System32 directory mode two: String path = ESAMServer.class.getResource ("/"). GetPath () + "/dll";
System.load (path+ "/mynative.dll"); Nativelibrary.addsearchpath ("MyNative.dll", Path);
JNA Technology Decryption JNA Working principle JNA is a Java class library built on the basis of JNI technology, which allows you to easily access functions in a dynamic-link library using Java directly.
Using JNI, you must manually write a dynamic-link library in C that maps the Java data types in the C language.
In JNA, it provides a dynamic C language-written forwarder that automatically implements data type mappings for Java and C. You no longer need to write a C dynamic link library.
Of course, this also means that using JNA technology can have a slight performance penalty than invoking a dynamic-link library using JNI technology. The speed may be several times lower. But the impact is small.
JNA Technical Difficulties
1, the current path is under the project, not the bin output directory.
2, the corresponding relationship of data structure:
Table of JAVA-C and operating system data types Java type C type Native representation
boolean int 32-bit integer (customizable)
byte char 8-bit integer
Char wchar_t platform-dependent
Short Short 16-bit integer
int int 32-bit Integer
Long Long Long, __int64 64-bit integer
float float 32-bit floating point
Double double 64-bit floating point
Buffer Pointer Pointer platform-dependent (32-or 64-bit Pointer to memory)
<t>[] (array of primitive type) pointer array 32-or 64-bit pointer to memory (ARGUMENT/RETURN) contiguous memory (s Truct member)
In addition to the above types, JNA also supports mapping of common data types.
String char* nul-terminated Array (native encoding or jna.encoding)
WString wchar_t* nul-terminated Array (Unicode)
String[] char** null-terminated array of C strings
Wstring[] wchar_t** null-terminated array of wide C strings
Structure struct* struct pointer to struct (argument or return) (or explicitly) struct by value (member of struct) (or exp licitly)
Union Union same as Structure
Structure[] struct[] array of structs, contiguous in memory
Callback <T> (*FP) () function pointer (Java or native)
Nativemapped varies depends on definition
Nativelong Long platform-dependent (32-or 64-bit integer)
PointerType pointer Same as pointer
JNA Programming Process
JNA a dll/.so file as a Java interface.
A DLL is a collection of C functions, a container, which is consistent with the concept of an interface.
We define such an interface,
Public interface TestDll1 extends Library {
/**
* The current path is under the project, not the bin output directory.
*/
TestDll1 INSTANCE = (TestDll1) native.loadlibrary ("TestDll1", Testdll1.class);
public void Say (WString value);
}
If the DLL outputs a function in stdcall mode, it inherits Stdcalllibrary. Otherwise, the default library interface is inherited.
A common static constant is required inside the interface: instance.
TestDll1 INSTANCE = (TestDll1) native.loadlibrary ("TestDll1", Testdll1.class);
With this constant, an instance of this interface can be obtained, thereby using the method of the interface. The function that calls the external DLL!
Attention:
The 1,native.loadlibrary () function has 2 parameters:
The name of the 1,dll or. so file, but without the suffix name. This is consistent with the JNI specification because the suffix name is not allowed to cross the operating system platform.
The path to the search DLL is:
1) Root Path of the project
2) The global path of the operating system,
3) The path specified by path.
2, the second parameter is the class type of this interface.
JNA uses this class type to dynamically create an instance of an interface based on the specified dll/.so file.
2, in the interface you just need to define the function or public variables you need, and what you don't need can be undefined.
public void Say (WString value);
The type of the parameter and the return value should be the same as the type of the C function in the DLL.
This is the difficulty of JNA, and even all cross-platform calls.
Here, the function parameters of the C language are: wchar_t*.
The corresponding Java type in JNA is wstirng.
All cross-platform, cross-language difficulties have cross-language, cross-platform development programmers are aware that cross-platform, language calls the difficulty, is the different languages caused by inconsistent data types. The failure of most cross-platform calls is the result of this problem.
In this connection, no matter what language or technical programme, this problem cannot be solved.
This requires careful development and design by the programmer. This is the responsibility of the programmer.
Common cross-platform calls are: 1,java calls the C-language DLL, and the. So dynamic link library functions.
2,. NET calls the functions in the DLL,. So dynamic link library written in C by P/invoke.
3, through WebService, in C,c++,java,. NET, among other languages.
WebService is the data that is passed in XML format.
Even powerful P/invoke or webservice can encounter a lot of difficulties in encountering complex data types and large data volumes.
Because of the complex data types of a language, it is difficult to express it in another language. This is the nature of cross-platform invocation issues. For example, WebService calls in many languages, such as Java. NET has an auto-implemented java/. NET type and XML type are mapped to a class library or tool.
However, in a real-world programming environment, these automatic conversion tools are often inadequate if the types are very complex.
Either Object-xml mapping errors.
Either map out a lot of memory.
Therefore, I personally do not quite catch a cold on these object-xml mapping frameworks.
I now use WebService to extract data-building objects from XML directly using the XML processing tool. Or, conversely, build XML data manually based on the attribute values in object.
The same is true of the invocation problem between Java and C language.
Java to invoke C-language functions, you must strictly follow the C language requirements of the amount of memory to provide Java-formatted data. The data type of the C language is perfectly modeled with the Java data type.
JNA has provided a large number of types that match the C language data type.
Cross-platform, cross-language invocation of the first principle: is to use basic, simple data types, as little as possible across the language, platform to pass data! Only you can save yourself.
If you have a complex data type and a large cross-platform data transfer in your program. Then you have to write some additional façade interfaces, simplifying the data types that need to be passed, simplifying the amount of data that needs to be passed.
Otherwise, both the difficulty of implementation and the performance of the program can be difficult to improve.
JNI still can't waste we've seen JNA's power. How shabby the JNI is compared to it!
However, there are some requirements that must be resorted to by JNI.
JNA is a framework built on the basis of JNI technology.
Using JNI technology, you can not only implement Java access to C functions, but also implement C language calling Java code.
and JNA can only implement Java access C function, as a Java framework, naturally cannot implement the C language calls Java code. At this point, you still need to use JNI technology.
JNI is the foundation of JNA. is the technical basis for Java and C interoperability.
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/shendl/archive/2008/12/23/3589676.aspx
Java-jna calling DLL (GO)