简体   繁体   English

JNI Java 使用以对象为参数的 DLL 函数

[英]JNI Java using DLL function which takes an object as param

I will need to use an external, unknown DLL and build a java wrapper around it.我需要使用一个外部的、未知的 DLL 并围绕它构建一个 java 包装器。 I do not have the DLL nor the header file at the moment, and maybe will not even get the header-file, but I'd like to prepare myself.我目前没有 DLL 和头文件,甚至可能不会得到头文件,但我想自己做好准备。 (I have no experience with C++ ) (我没有使用 C++ 的经验)

Following situation:以下情况:

Let's say this DLL has a function, which contains one or more C++ classes as method signature.假设这个 DLL 有一个函数,它包含一个或多个 C++ 类作为方法签名。 Then how could I call this function with JNI, as in my java project those custom classes in the DLL are non-existent?那么我怎么能用 JNI 调用这个函数,因为在我的 java 项目中,DLL 中的那些自定义类是不存在的? Is there a option to "clone" or "port" the C++ class to java?是否可以选择将 C++ 类“克隆”或“移植”到 Java? Is there anything I could do with a tool like Dependency Walker to resolve this problem?我可以用像 Dependency Walker 这样的工具来解决这个问题吗?

What would be the best / most simple approach to accomplish that?实现这一目标的最佳/最简单的方法是什么?

Here is some code which I already tried, to find out how it behaves:这是我已经尝试过的一些代码,以了解它的行为方式:

Java Class with main Java 类与主要

public class FourthJNI {

    public static native int returnAgeOfHuman(int zuQuadrierendeZahl);

    public static void main(String[] args) {
//      /* This message will help you determine whether
//        LD_LIBRARY_PATH is correctly set
//       */
//      System.out.println("library: "
//              + System.getProperty("java.library.path"));

        Human testHuman = new Human("abcde", 23, "M");

        /* Call to shared library */
        int ageOfHuman = FourthJNI.returnAgeOfHuman(5);
        System.out.println(testHuman.toString());
        System.out.println("Age: " + ageOfHuman);
    }
} 

generatd h-file生成 h 文件

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class FourthJNI */

#ifndef _Included_FourthJNI
#define _Included_FourthJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     FourthJNI
 * Method:    returnAgeOfHuman
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_FourthJNI_returnAgeOfHuman
  (JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif

The best way here is to use adapter pattern ( https://en.wikipedia.org/wiki/Adapter_pattern ).这里最好的方法是使用适配器模式( https://en.wikipedia.org/wiki/Adapter_pattern )。 Inside your JNI code you have to call DLL by creating all the objects, as expected by C++ API.JNI代码中,您必须按照C++ API 的预期,通过创建所有对象来调用DLL

You can find sample here: http://jnicookbook.owsiak.org/recipe-No-021/ and here https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025您可以在这里找到示例: http : //jnicookbook.owsiak.org/recipe-No-021/和这里https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025

You can also take a look at the code where one shared library ( JNI ) calls the code from another one: http://jnicookbook.owsiak.org/recipe-No-023/您还可以查看一个共享库 ( JNI ) 从另一个共享库调用代码的代码: http : //jnicookbook.owsiak.org/recipe-No-023/

Basically, what you have to do is to create JNI based wrapper code that translates Java calls to native methods into C++ calls, and vice versa - the code that translates return values into something that is expected by Java.基本上,你所要做的是创建JNI基于包装,转换代码Java调用本地方法为C++调用,反之亦然-即转化返回值到的东西,是由预计的Java代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM