I took on a project some time ago, which required the use of a dll written in C# provided by a third party. The project itself is in the Java language, so the following article is written. This article introduces to you how to use jna calls in Java. The relevant information about DLL in C# is introduced in great detail through sample code. Friends in need can refer to it. Let’s take a look together.
Preface
JNA (Java Native Access) provides a set of Java tool classes for dynamically accessing the system's native library during runtime : such as Window's dll) without writing any Native/JNI code. Developers only need to describe the functions and structures of the target native library in a Java interface, and JNA will automatically map the Java interface to the native function.
Advantages
JNA allows you to call local methods directly like calling normal java methods. It is almost the same as directly executing a local method, and calling a local method does not require additional processing or configuration, nor does it require redundant references or coding, making it very convenient to use. The biggest benefit of JNA is that it avoids the time-wasting behavior of reinventing the wheel.
Everyone who has used JNI knows that JNI is a very abnormal design. JNI needs to import the code generated specifically for Java. The header file is an intrusive design, so that the forcibly modified source code compiled dll will not be called by C
#JNA description
JNA class library uses a small local class library sub to dynamically call local code. Programmers only need to use a specific Java interface to describe the structure and some basic properties of the native code method to be called. This saves a lot of configuration and compilation code to adapt to multiple platforms. Because all interfaces in the public jar package provided by JNA are called.
1. Description of requirements:
If our project is developed using c#, we will need to cooperate with the java team in the later stages of development, some of which The business logic has been code completed using C#, then we may consider using Java to call the ready-made C# dll to implement the requirements. I happened to encounter such a problem at work a few days ago, so I wrote down the development process.
Of course, this is just a hypothesis. After analyzing the specific situation, I personally think that refactoring the code is the best way...
2. Principle description:
Actually, I don’t quite understand the specific principles, so I’ll just talk about it based on my own understanding.
Because the C# code is hosted on the .net platform, Java cannot directly call the C# code, so C++ middleware is introduced. The C++ project can set the project to the clr public runtime, thereby calling C# by reference. Corresponding method. And jna can directly call the dll generated by c++, so the general process is smooth. C++ calls the written c# dll, and java then calls the dll middleware generated by c++. The general process is like this, but there are many pitfalls, which I will elaborate on below.
3. Running platform:
System: Windows 10 x64
Development tools: Visual Studio 2015/2017 (I I have implemented different versions installed on laptops and company computers) MyEclipse2014
SDK: jdk-x86, jdk-x64 (dll is divided into x86 and x64 platforms, and the version of jdk must correspond to the same computer. The two versions of jdk are quite annoying. I use the system configuration jdk32-bit to debug 32-bit dll, and then myeclipse comes with 64-bit jdk to debug 64-bit dll)
4. Preparation:
1. First prepare the above-mentioned operating platform. It is recommended to choose a jdk with the same number of digits as the system (install vs, myeclipse or eclipse or sts);
2. Download jna.jar: JNA download (download jna-4.4.0.jar and jna-platform-4.4.0), you can also download it locally
5. Start CODE
5.1 Generate c#DLL
5.1.1 Start vs as administrator (the project involves registering com components, which must be started as an administrator to complete), Create a new c# project
5.1.2 Set up a c# project
First, right-click the newly created Invoke project and click Properties.
Continue setting project properties.
Remember to save.
Then create a new CSharp class code that needs to be called. Here we create some simple methods. In order to demonstrate the effect, we operate on int, string and bool respectively. As shown in the picture:
Then right-click the project and click Generate.
First step, completed, well done.
5.2 Generate c++ middleware
5.2.1 Create a new c++ project and set properties
Project, select properties.
## 5.2.2 Writing c++ code
Add cpp file
Edit cpp file
Okay, all the work of c++ and c# is completed, right-click to generate.
Copy the full name of the dll generated file and use it in java later.
6.2 Start writing java code
Then we run:
As shown in the figure, we copy Invoke.dll to the bin directory of jdk:
Then run:
nice! Commonly used types such as int, string, and boolean can be transferred smoothly. In fact, other types can also be implemented, as long as the type correspondence between different languages is followed. The specific type relationship can be found on Baidu.
Possible reasons: 1. The c#dll is not copied to the bin directory of jdk; 2. The data types between java and c++ do not correspond;
7.1.2 java error: Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'D:\vs workplace\X86InvokeTest\Release\X86CPPDlls': Native library (win32-x86/D: \vs workplace\X86InvokeTest\Release\X86CPPDlls.dll) not found in resource path ([file:/G:/My%20Eclipse%20workplace/InvokeCSharpX86Test/bin/, file:/G:/My%20Eclipse%20workplace/InvokeCSharpX86Test/ Lib/jna-4.4.0.jar, file:/G:/My%20Eclipse%20workplace/InvokeCSharpX86Test/Lib/jna-platform-4.4.0.jar])Possible reasons:
1. The c++dll path is incorrect. It is recommended to use the absolute path when doing tests, so that you can call it directly in the java program without copying after the c++ project is compiled;
2. JDK platform and The platform of the c++ project does not match. If jdk is 32-bit, then the c++dll must also be 32-bit, and the same is true for 64-bit;
7.1.3 The test of 32-bit dll compiled under windows64-bit failed, it is not clear at the moment Is it because of the 64-bit system? Since the virtual machine is not installed on my computer, I did not test it on the 32-bit system.
Summarize
The above is the detailed content of Example of how Java uses jna to call dll in c#. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
