从Java调用C ++ dll [英] Calling C++ dll from Java

查看:102
本文介绍了从Java调用C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java作为一个小应用程序。这是对现有MFC项目的重写。有一个现有的DLL,我需要更改,以使用JNI访问Java。所有这些Java的东西对我来说都是新的,所以当我读到其他论坛帖子时,我有点麻烦,感觉相当密集。在现有的DLL中,我有一个这样的功能:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆