Java call hardware interface

There are usually three schemes for using Java to call DLL dynamic link libraries: JNI, Jawin, Jacob. Among them, JNI (Java Native

Interface) is a method provided by the Java language itself to call a locally compiled function library. It is cross-platform and can be used in Different

local libraries are called on different machines. Both Jawin and Jacob are open source projects of sourceforge.net, which are based on JNI technology and rely on

the realization of Wi ndows, making it more convenient to use COM and DLL under the Windows platform.

1. JNI (Java Native Method Invocation)
    The application scheme of JNI is based on the mapping between Java classes and local functions. The steps to use DLL are relatively troublesome, not only involving

Java programming, but also C/C++ programming.

    The steps to use JNI are:

1. Write a Java class, and use this class to declare the function services provided by the DLL. The Java methods are declared as native, and the method

signature can be customized without implementing the function body.

2. Use the Javah tool to generate the corresponding .h header file for the Java class.

3. The most important troublesome step: preparation of C / C ++ code that implements the function in a .h file declared, the C / C ++ header file code comprising jni.h

member, and wherein the defined data types used when writing code Program as the input and return data type of the function. Use this method to

achieve data type conversion. In this step, you can call DLL libraries that already exist in the C / C ++ code (when someone gives us a current

into the dll control, we only know the interface, you can not change its implementation will require "custom dll "Call "off-the-shelf dll")

4. You can use this Java class when you write another Java code.

In the third step, when writing C/C++ functions, you can use an env pointer called interface pointer to call a series of (

many) functions provided by JNI , and use these functions to access JVM objects and data.

Disadvantages of using JNI: it is troublesome to use, need to encapsulate the existing DLL, and need to have a better understanding of C/C++.

The advantage of using JNI: You can call local libraries across platforms.

2. Jawin

official website: http://jawinproject.sourceforge.net/

Jawin's application scheme is based on using the original byte stream to transfer data when calling functions. That is, after specifying a certain function in a DLL in Java, it is

passed to the

parameters required by the DLL function through the original byte stream (the number of storage bytes occupied by the parameter data type and the byte sequence used by the system need to be considered) . The return value is also obtained by parsing the original byte stream to obtain the correct value.

Jawin has a Jawin.dll and Jawin.jar, which require different configurations according to different development environments.

Disadvantages of using Jawin: it is not convenient to debug, almost all errors throw the same exception COMException; need to have a

better understanding of data type conversion ; Can not be cross-platform, relatively strong dependence on Windows.

The advantages of using Jawin: easy to use, without C/C++ development, and easy to use without encapsulating the original DLL.

My personal experience: When I used Jawin to call the single-point dll, I encountered errors many times, and finally failed to debug. Later, I checked its official documentation and

found that it does not support some com calls.

Three, Jacob

Official document: http://danadler.com/jacob/

Jacob is the abbreviation of Java-Com Bridge and can also be used to call DLL. The bottom layer is also implemented using JNI and also has the

platform dependency of Windows .

The control is very convenient to call, you can call com and active, and it can be done in a few minutes. After it is downloaded, there is a dll and a jar package.

Just put the dll under WIN_HOME/system32/, and put the jar package under the classpath of the project. .

The following is an example of my calling single point dll:

 
  1. import com.jacob.com. *; 
  2.  
  3.  
  4. public class CallSSO { 
  5.     private static Dispatch dif; 
  6.     //Load single point control SSOCrypto.SSOCrypt.1 
  7.     static { 
  8.          dif = new Dispatch("SSOCrypto.SSOCrypt.1"); 
  9.     } 
  10.     //Generate a random code, call the single-point interface SSORadomGen, the passed parameter is strPlainString 
  11.     public static int SSORadomGen(String strPlainString) 
  12.     { 
  13.           Variant radom = Dispatch.call(dif, "SSORadomGen", new Variant(strPlainString)); 
  14.           return radom.getInt (); 
  15.     } 
  16. //Get the encrypted string, call the single-point interface SSOEncrypt, and pass parameters are strPlainString, lngRandomnum, strSvrCert 
  17.     public static String SSOEncrypt(String strPlainString, 
  18.                                            int lngRandomnum) 
  19.     { 
  20.         String strSvrCert ="1BQYR-YLGXV-QM439-CJSCS-TCN6V"; 
  21.         Variant encrypt = Dispatch.call(dif, "SSOEncrypt", new 
  22.  
  23. Variant(strPlainString),new Variant(lngRandomnum),new Variant(strSvrCert)); 
  24.         return encrypt.getString(); 
  25.     } 


Disadvantages: not cross-platform, relatively strong dependence on Windows.

Advantages: It is very convenient to call, without C/C++ development, and easy to use without encapsulating the original DLL.

Summary: The above three methods are the commonly used java calling dll methods. I think the third method is easier to use. Of course, each has advantages and disadvantages

, as well as performance problems. I have not tested and compared. Please try it yourself.

Guess you like

Origin blog.csdn.net/u010460625/article/details/108145191