Java (JNA) calls C + + dynamic libraries (DLLs)

Source: Internet
Author: User

JNA (Java Native Access): Is a Java open-source framework built on JNI, sun-led development, used to invoke C, C + + code, especially the underlying library files (called DLL files in Windows,. so files under Linux).

Notice a few questions first:

1. Download the Jna.jar package, Jna-platform.jar can not, but it is helpful to debug the code Platform.jar for Jna.jar is a supplement and extension, Jna.jar equivalent to the kernel, Platfrorm.jar equivalent to incremental plug-ins.

2. Platform for dynamic link library compilation. If it is compiled via the x86 platform, it can only be loaded using the 32-bit JDK environment, and if it is a 64-bit JDK, the dynamic library must be compiled using the x64 platform;

3. Pay more attention to the location of the DLL when calling.

Code Start:

For Java, it is common to call DLL dynamic libraries that have been built. So LZ does not post the generation of dynamic library. If necessary, Baidu can be a bit.

 Public classJnatestdll { Public InterfaceDllextendslibrary{Dll Instance= (DLL) native.loadlibrary ("Hidcomm", DLL.class);  Public BooleanLoadhidcomm ();  Public BooleanHidwritedata (String buf,intLen); }     Public Static voidMain (string[] args) {BooleanLoad =Dll.instance.LoadHidComm (); if(load) {String buf= "WEIXIN://WXPAY/BIZPAYURL?PR=FCT5HBP"; intLen =buf.length (); BooleanEWM =Dll.instance.HidWriteData (buf, Len);        System.out.println (EWM); }    }}

This is the normal Java project call test, LZ is to put DLL dynamic library in the root directory of the project, is to select the project and then copy it ok.

The following example is an example of invoking DLL dynamic libraries in a Web project:

 Public classJnacalldllservice {Private StaticString Path = System.getproperty ("User.dir"). Concat (File.separator). Concat ("Lib"). Concat (File.separator);  Public InterfaceSerialanddllnewextendsLibrary {serialanddllnew instance= (serialanddllnew) native.loadlibrary (Path.concat ("Zzcard"), Serialanddllnew.class);  Public intIcc_opencomport (intNport, String Gate,intBaudinttimeout);  Public intIcc_poweron (intcard_no,bytebyreference atr,shortbyreference Ictype); }}

The path section is where the dynamic library is, because the LZ application is placed in a Linux environment. LZ is to put the dynamic library under the Lib, so easy to deploy, simple and convenient. As shown in the following:

LZ is tested first, and then used in the project, both of the above methods are successful.

Java (JNA) calls C + + dynamic libraries (DLLs)

Related Article

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.