Use JNA to protect your legacy code (II)

Source: Internet
Author: User

referencing DLL code from Java

List Four demo code reference DLL functions

public interface CLibrary extends Library {
  CLibrary INSTANCE1 = (CLibrary)
  Native.loadLibrary((Platform.isWindows() ? "nativecode" : "c"),
   CLibrary.class);
  int helloWorld(int divider);
  }
  public static void main(String[] args) {
  CLibrary.INSTANCE1.helloWorld(77));
  }

Listing four, accessing DLL functions

In list four, the Clibrary instance is created. This object allows the specified DLL to be downloaded. Next, is the loading process of the library, which needs to be marked from the library--in the case of listing four, there is only one flag, called HelloWorld ().

List five shows the program from listing four code

C:\jnacode>java HelloWorld
  Value is 1

Listing five, the code for the calling DLL

In the list V there's nothing to be surprised about--the value 77 is allowed into the function. Then inside the function, the parameter (77) is removed by 77, and the answer is: 1.

When I try to solve the DLL problem and relate to the calling convention, I want to look at the process of building the DLL. Fortunately, you can achieve this through a tool called Dependency Walker, through dependency Walker you can see the DLL generation process. To achieve this, you need to download a free copy of Dependency Walker, open it, and then load the DLL inside. You can see similar content as shown in Figure one.

Figure I, DLL internal information

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.