如何在 Java 中加载、访问和使用C++ DLL 函数?

How to load, access, and use C++ DLL functions in Java?

本文关键字:C++ DLL 函数 访问 Java 加载      更新时间:2023-10-16

我有一个在C++中创建的DLL。这非常简单。DLL 旨在执行加法、减法、乘法和除法。这是代码:

数学DLL

.cpp
#include "Math.h"
using namespace std;
long addition(long a, long b) {
return a + b;
}
long subtraction(long a, long b) {
return a - b;
}
long multiplication(long a, long b) {
return a * b;
}
long division(long a, long b) {
// Make sure neither variables are 0 to avoid errors
if (b != 0 && a != 0)
return a / b;
else
return 0;
}
int testReturn() {
return 7;
}

数学 DLL.h

#ifdef MATH_EXPORTS
#define MATH_API __declspec(dllexport)
#else
#define MATH_API __declspec(dllimport)
#endif
extern "C" MATH_API long addition(long a, long b);
extern "C" MATH_API long subtraction(long a, long b);
extern "C" MATH_API long multiplication(long a, long b);
extern "C" MATH_API long division(long a, long b);
extern "C" MATH_API int testReturn();

编译后,此代码为我提供了Math DLL.dllMath DLL.lib的文件。

我知道您可以在另一个C++程序中运行它,如下所示:

#include <iostream>
#include <Windows.h>
#include "tchar.h"
typedef int(__stdcall* importFunction)();
using namespace std;
int main() {
HINSTANCE hGetProcIDDLL = LoadLibrary(_T("Math DLL.dll"));
if (hGetProcIDDLL == NULL) {
cout << "Cannot locate the DLL file." << endl;
}
else {
cout << "Found the DLL file!" << endl;
}
importFunction testReturn = (importFunction)GetProcAddress(hGetProcIDDLL, "testReturn");
if (!testReturn) {
cout << "Could not locate the function" << endl;
}
else {
cout << "testReturn() returned " << testReturn() << endl;
}
}

但是如何在Java中使用Eclipse Photon做到这一点呢?

这是一个有点过程,但它似乎对我来说效果很好。首先,从这里下载 JNA 库。在zip中,您将找到.jar文件。在 Eclipse 中的 Java 项目中包含此内容,方法是右键单击您的项目并转到构建路径>配置构建路径

这应该会自动将您带到"库"选项卡,但如果没有,请单击它。然后点击右侧的添加外部 JAR按钮并找到 JNA.jar文件。

现在您的项目中已经有了必要的库,请创建一个名为"MathDLL"的新类。从这里,您可以输入以下代码:

import com.sun.jna.Library;
import com.sun.jna.Native;
/** Simple example of native library declaration and usage. */
public class MathDLL {
public interface mathDLL extends Library {
mathDLL INSTANCE = (mathDLL) Native.loadLibrary("Math DLL", mathDLL.class);
// Function definition
long addition(long a, long b);
long subtraction(long a, long b);
long multiplication(long a, long b);
long division(long a, long b);
}
public static void main(String[] args) {
mathDLL dll = mathDLL.INSTANCE;
// 500 + 531
long additionResult = dll.addition(500, 531);
// 250 - 12
long subtractionResult = dll.subtraction(250, 12);
// 12 * 12
long multiplicationResult = dll.multiplication(12, 12);
// 400 / 20
long divisionResult = dll.division(400, 20);
System.out.println("Addition (should be 1031): " + additionResult + ", Subtraction (should be 238): " + subtractionResult + ", Multiplication (should be 144): " + multiplicationResult + ", Division (should be 20): " + divisionResult);
}
}

此代码会将您的 DLL 添加到您的代码中(确保您的 DLL 位于您的 Java 项目运行的同一文件夹中),定义函数,在编译期间在 DLL 中找到它们,并允许您在 Java 中轻松输入值并获取返回值!

常见错误

  • 确保 DLL 项目与系统的相同体系结构匹配。x86 计算机需要 x86 DLL 才能在 Java 中成功运行。x64 计算机需要一个 x64 DLL。仔细检查您是否将其导出为正确的体系结构。
  • 在 Java 中,不要在文件名后加上.dll。程序知道它正在寻找一个.dll,如果你提供一个错误,它会返回一个错误。
  • 确保函数定义和参数
  • 始终与实际 DLL 中声明的函数定义和参数对齐。

我知道我回答了我自己的问题,但我希望我能帮助某人更轻松地获得他们正在寻找的答案,减少跑来跑去。