Create DLL dynamic-link libraries and use Java to invoke

Source: Internet
Author: User

Reference article: http://www.cnblogs.com/matthew-2013/p/3480296.html

http://blog.csdn.net/g710710/article/details/7255744

First, we discuss what is dynamic link library, according to the explanation of the Hundred Poison Encyclopedia:

The dynamic link library or the Dynamic-link Library, abbreviated as DLL, is a way for Microsoft to implement the shared library concept in Microsoft's Windows operating system. These library functions have the extension ". dll", ". ocx" (a library with ActiveX control) or ". DRV" (Legacy system drivers). Dynamic linking provides a way for a process to invoke a function that is not part of its executable code. The executable code for a function is in a DLL file that contains one or more functions that have been compiled, linked, and stored separately from the process that uses them. DLLs also help to share data and resources. Multiple applications can access the contents of a single copy of a DLL in memory at the same time. Using a dynamic-link library makes it easier to apply updates to individual modules without affecting other parts of the program. For example, if you have a large online game where the entire hundreds of MB or even gigabytes of game code is placed in one application, future revisions will be time consuming, and if the code for different features is placed in several dynamic link libraries, you can apply the update without rebuilding or installing the entire program. 】

VS2012 Create project, WIN32-WIN32 project, name Mydll. Application settings, select the DLL option.

New Header File Testdll.h

#ifndef Testdll_h_#defineTestdll_h_#ifdef Mylibdll#definemylibdll extern "C" _declspec (dllimport)#else#definemylibdll extern "C" _declspec (dllexport)#endifMylibdllintADD (intPlus1,intplus2);//can also write like this://extern "C" {//_declspec (dllexport) int Add (int plus1, int plus2);//};#endif

New CPP Testdll.cpp

" stdafx.h "  "testdll.h"<iostream>using namespace  STD; int ADD (intint  plus2) {    int add_result = plus1 + plus2;     return Add_result;}

Create a new mydll.def in the source file directory

" MyDLL " Exportsadd @ 1

Dllmain.cpp is automatically created and defines the portal of the DLL application

//dllmain.cpp: Defines the entry point for the DLL application. #include"stdafx.h"BOOL apientry DllMain (hmodule hmodule, DWORD Ul_reason_for_call, LPVOID lpreserved) {Switch(ul_reason_for_call) { CaseDll_process_attach: CaseDll_thread_attach: CaseDll_thread_detach: CaseDll_process_detach: Break; }    returnTRUE;}

After that, compile, build.

The MyDLL.dll of production can be found in the workspace Mydll\debug catalogue.

Place the DLL file in the bin directory of the JDK.

Open Eclipse and feel free to create a new project. Testdll

Join the Jna-4.2.2.jar package of dependent JNA.

Write code:

 PackageTestdll;Importcom.sun.jna.Native;Importcom.sun.jna.win32.StdCallLibrary;/** * @authorSonne*/ Public classJNA { Public InterfaceClibraryextendsstdcalllibrary {clibrary INSTANCE= (clibrary) native.loadlibrary ("MyDLL.dll", clibrary.class); intADD (intPlus1,intplus2); }     Public Static voidMain (string[] args) {intresult = CLIBRARY.INSTANCE.ADD (); System.out.println ("Result is" +result); }}

The Java interface needs to be defined here, and the method in this interface is consistent with the DLL library function method, and invoking this interface through JNA is actually called DLL dynamic link library. The run result is, 3.

This is the Add method that called the DLL. Get 1+2=3.

Create DLL dynamic-link libraries and use Java to invoke

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.