如何在 Java 中加载、访问和使用C++ DLL 函数?
How to load, access, and use C++ DLL functions in Java?
我有一个在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.dll
和Math 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 中声明的函数定义和参数对齐。
我知道我回答了我自己的问题,但我希望我能帮助某人更轻松地获得他们正在寻找的答案,减少跑来跑去。
相关文章:
- 挂起和取消挂起一个文件DLL
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 导入库可以跨dll版本工作吗
- 从C++dll访问C#中的一行主要参数
- 链接到自行创建的dll失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- C++:将外部库链接到dll库
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 不同的Visual Studio版本中缺少.dll
- 从DLL中删除类的实例
- 如何包装第三方DLL在R中使用
- 使用c#访问c++dll中带有char*参数的函数时发生AccessViolationException
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Python ctypes:不会按预期加载 dll
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL