从Java调用C ++ dll [英] Calling C++ dll from Java
问题描述
externC__declspec(dllexport)bool Create()
{
return TRUE;
}
愚蠢的问题时间。如何正确设置为Java调用?
我尝试过:
JNIEXPORT jboolean JNICALL Create()
{
return TRUE;
}
我正在包括jni.h,所有的编译都很好。但是,当我从Java调用它时,我得到了UnsatisfiedLinkError。我使用这种方式从Java调用它:
public static native boolean CreateSession();
System.load(D:\\JavaCallTest.dll);
Create();
有人可以把我推向正确的方向吗?我真诚地感谢任何帮助。
谢谢,
Nick
您需要在本地代码中包含Java类名称和路径,例如,如果您的本地方法在Java中被声明为:
public class NativeCode {
public static native boolean CreateSession();
}
,类路径为(例如) com .example.NativeCode
您将以本地方式声明方法如下:
externC
JNIEXPORT jboolean JNICALL Java_com_example_NativeCode_CreateSession(JniEnv * env,jclass clazz)
{
return JNI_TRUE;
}
所有JNI方法都有一个JNIEnv指针和类作为其前两个参数。 / p>
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;
}
Dumb question time. How do I properly set it up to be called by Java?
I tried this:
JNIEXPORT jboolean JNICALL Create()
{
return TRUE;
}
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.
Thanks,
Nick
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();
}
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;
}
All JNI methods have a JNIEnv pointer and class as their first two parameters.
这篇关于从Java调用C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!