从 Java 调用 C++ dll [英] Calling C++ dll from Java
问题描述
我将 Java 用于一个小型应用程序.它是对现有 MFC 项目的重写.我需要更改现有的 dll 才能使用 JNI 从 Java 进行访问.所有这些 Java 东西对我来说都是新的,所以当我阅读其他论坛帖子时,我遇到了一些麻烦并且感觉相当密集.在现有的 dll 中,我有一个这样的函数:
I'm using Java for a small app. It's a rewrite of an existing MFC project. There is an existing dll that I need to change to enable access from Java using JNI. All of this Java stuff is new to me, so I'm having a little trouble and feeling rather dense when I read other forum posts. In the existing dll I have a function like this:
extern "C" __declspec(dllexport) bool Create()
{
return TRUE;
}
愚蠢的提问时间.如何正确设置它以供 Java 调用?
Dumb question time. How do I properly set it up to be called by Java?
我试过了:
JNIEXPORT jboolean JNICALL Create()
{
return TRUE;
}
我包含了 jni.h,一切都可以正常编译.但是,当我从 Java 调用它时,我得到了 UnsatisfiedLinkError.我使用这个从 Java 调用它:
I'm including jni.h and everything compiles fine. However, when I call it from Java I get UnsatisfiedLinkError. I'm calling it from Java using this:
public static native boolean CreateSession();
System.load("D:\JavaCallTest.dll");
Create();
有人可以把我推向正确的方向吗?我真诚地感谢任何帮助.
Could someone kindly push me in the proper direction? I sincerely appreciate any help.
谢谢,
尼克
推荐答案
你需要在你的本地代码中包含 Java 类名和路径,例如如果你的本地方法在 Java 中声明为:
You need to include the Java class name and path in your native code, for example if your native method was declared in Java as:
public class NativeCode {
public static native boolean CreateSession();
}
并且类路径是(例如)com.example.NativeCode
,您将在本机中声明您的方法,如下所示:
and the class path was (for example) com.example.NativeCode
you would declare your method in native as follows:
extern "C"
JNIEXPORT jboolean JNICALL Java_com_example_NativeCode_CreateSession(JniEnv* env, jclass clazz)
{
return JNI_TRUE;
}
所有 JNI 方法都有一个 JNIEnv 指针和类作为它们的前两个参数.
All JNI methods have a JNIEnv pointer and class as their first two parameters.
这篇关于从 Java 调用 C++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!