Wednesday, May 5, 2021

In this article I will create a simple Calculator for example.

- Math function are implemented in DLL (add/sub/mul/dev)

- Use java swing to create GUI

- Use JNA java libary to call math function from DLL


1. Create math C/C++ DLL library

- Open Microsoft Visual Studio

- File > New > Project > Visual C++ > Windows Desktop > Dynamic-Link Library with exports (DLL) 


- Add your source
    Create MyMath.dll with Add/Sub/Mul/Dev function


- Build DLL
    In this test, build Release/x64
    


    MYMATH.dll will be exported to x64/Release folder


2. Download java JNA library
- Donwload load jna-x.x.x.jar and jna-platform-x.x.x.jar from github
    https://github.com/java-native-access/jna
- JNA wikipedia
    https://en.wikipedia.org/wiki/Java_Native_Interface
- JNA example code
    https://www.eshayne.com/jnaex/index.html

3. Create java GUI application window
   Reference below article to create GUI app and add JNA library to project

- Create GUI
- Add jna-x.x.x.jar and jna-platform-x.x.x.jar to project

- Add JNA interface that define function relative with DLL function


- Add click event for Calculate button and insert Add/Sub/Mul/Dev function what defined in JNA interface


- Put MYMATH.dll into bin folder

- Click Run button in eclipse to run up application

4. Export
- Export java project to runnable jar file

- Copy MYMATH.dll to folder where exported jar file

- Double click on Calculator.jar to use application


※NOTE:
If you put all the C# and C++ dll files in the java project directory, an error will occur. The reason is that although java can find the C++ dll file, but the C# dll file cannot be found, you need to add the C# dll The file is placed in the bin folder under the jdk installation directory of the running machine. problem solved.
Repeat again. The resulting dll file should be consistent with the version of the jdk installed on the running machine (x86 or x64), otherwise a call error will occur.






Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -