[英]Call a 86x Dll from Java
Am trying to call a x86 DLL that I created using VC 6, from a java project on Eclipse, first try I got an error saying that I can't call a x86 DLL from a x64 envirement and that the DLL can't be loaded. 我试图从Eclipse上的Java项目中调用使用VC 6创建的x86 DLL,首先尝试出现一个错误,提示我无法从x64环境调用x86 DLL,并且无法加载该DLL。 。 So I installed a x86 jre and I have no more problem to charge the DLL.
因此,我安装了x86 jre,并且对DLL充电没有更多问题。 But when I try to call my c++ function I get the following exception:
但是,当我尝试调用c ++函数时,出现以下异常:
Exception in thread "main" java.lang.UnsatisfiedLinkError: mm.SimpleDLL.SimpleDLL_Calculation_Add(II)I
Can someone please help me ? 有人可以帮帮我吗 ? Thank you.
谢谢。
SimpleDLL.h SimpleDLL.h
#ifndef SIMPLE_DLL_H
#define SIMPLE_DLL_H
namespace SimpleDll
{
extern class Calculation
{
public:
static __declspec(dllexport) int Add(int a, int b);
};
}
#endif SIMPLE_DLL_H
SimpleDLL.cpp SimpleDLL.cpp
#include "SimpleDll.h"
namespace SimpleDll
{
int Calculation::Add(int a, int b)
{ return a + b; }
}
SimpleDLL.java SimpleDLL.java
package mm;
public class SimpleDLL {
static
{
System.load("D:\\SimpleDLL.dll");
}
public static void main(String ar[])
{
System.out.println("Hello world from Java");
SimpleDLL t=new SimpleDLL();
int x = t.SimpleDLL_Calculation_Add(6, 7);
System.out.println("Resultat "+x);
}
public native int SimpleDLL_Calculation_Add(int a, int b);
}
Exported DLL Functions View 导出的DLL函数视图
Resolved using the JNA library, I used this link to walkthrough 使用JNA库解决,我使用此链接进行了演练
You need to build a 32-bit DLL and a .h
file using javah
for the exact signature Java expects. 您需要使用
javah
构建32位DLL和.h
文件,以获得Java期望的确切签名。 It is usually something like 通常是这样的
JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
(JNIEnv *, jobject, jboolean);
from https://medium.com/@bschlining/a-simple-java-native-interface-jni-example-in-java-and-scala-68fdafe76f5f 来自https://medium.com/@bschlining/a-simple-java-native-interface-jni-example-in-java-and-scala-68fdafe76f5f
An alternative approach is to use a library like JNA or JNR-FFI which allow you to bind to a C library without writing this bridging code. 一种替代方法是使用JNA或JNR-FFI之类的库,这些库使您无需编写此桥接代码即可绑定到C库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.