Use JNA to call the local method instead of JNI

Source: Internet
Author: User

JNA, short for Java Native access, is a method developed by Sun to call local methods. Compared with JNI, JNA greatly simplifies the process of calling local methods and is more convenient to use, JNA is well-developed on the basis of JNI. It cannot be described as blue rather than blue. Let's take a look at JNI's call process:

 

To use JNI, you have to complete the above steps, which is troublesome. Instead, you can use JNA to save much trouble. Basically, you can do this without leaving the Java environment.

The JNA project home page is https://jna.dev.java.net/, and the latest version is 3.2.4. When downloading, remember to download the built-in example. jar, which provides some examples of JNA, so that you can better understand JNA.

To use JNA to call a local method, you need to customize the data structure. Next, let's call the locking workstation method provided by windows to learn about JNA.

1. First, query the Windows API to learn how to lock the workstation, which is defined in user32.dll. Then, define an interface to inherit the JNA library. java interface, used to declare the dll library file. Here we name it USER32:

Public interface USER32 extends library {}

2. query the API provided by user32.dll to learn that the locking method is lockworkstation and the return type is boolean. Add the corresponding method in user32.java:

Boolean lockworkstation ();

In this way, our user32.java class is defined. Next we will write a test program for calling.

3. Compile the test class, such as lockworkstation. Java, first load the corresponding DLL through the native class of JNA:

USER32 USER32 = (USER32) Native. loadlibrary ("USER32", user32.class );

Then you can call the lockworkstation method. The complete code is as follows:

Public class lockworkstation {
Public static void main (string [] ARGs ){
USER32 USER32 = (USER32) Native. loadlibrary ("USER32", user32.class );
User32.lockworkstation ();
}
}

The first parameter in the loadlibrary method is the name of the DLL file to be loaded. The second parameter is used by JNA to load the DLL file. The loading sequence is, first from users. find the current folder of the class. If not, find the Win32/win64 folder under the current folder of the project, and search for the corresponding DLL file, if you cannot find it and search for it under Windows, an exception will be thrown if you cannot find it. Put the file in the project root folder in twaindsm. dll in the following format:

 

The above USER32 defines dll library files and sometimes encounters data types such as handle, point, word, and MSG. Some data types are not provided in JNA and need to be defined by yourself, according to different roles, the parent classes inherited during definition are different. For example, the handle method is as follows:

Class handle extends pointertype {
Private Boolean immutable;
Public handle (){}
Public handle (pointer p) {setpointer (p); immutable = true ;}
Public object fromnative (Object nativevalue, fromnativecontext context ){
Object o = super. fromnative (nativevalue, context );
If (invalid_handle_value.equals (o ))
Return invalid_handle_value;
Return O;
}
Public void setpointer (pointer p ){
If (immutable)
Throw new unsupportedoperationexception ("immutable reference ");
Super. setpointer (P );
}
}

Handle is defined as a type-safe pointer. Point is used to represent coordinates. It does not need to be so complicated. The definition is as follows:

Class Point extends Structure {
Public int X, Y;
Public point (){}
Public point (int x, int y) {This. x = x; this. Y = y ;}
}


JNA may not always run smoothly. For example, "Invalid Memory Access" is thrown. Check whether the variable is = NULL. There is also the memory alignment problem. When image information is obtained from the memory for saving, if the memory alignment processing is poor, a very serious exception will be thrown, resulting in JVM abnormal exit, JNA provides four memory align_default, align_none, align_gnuc, and align_msvc. Align_default uses the default align_default align_none align_gnuc align_default align_none alig. Align_msvc is the memory alignment mode for Win32/msvc architectures.

JNA also provides a protection mechanism. for example, to prevent abnormal JNA from causing abnormal JVM exit, this function is enabled by default. setproperty ("JNA. protected "," true "); remember to call it before JNA loads the DLL file, and then try {...} catch (throwable E. JNA also provides a protection mechanism. for example, to prevent abnormal JNA from causing abnormal JVM exit, this function is enabled by default. setproperty ("JNA. protected "," true "); remember to call it before JNA loads the DLL file, and then try {...} catch (throwable E.

Use JNA to call the local method instead of JNI

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.