1

I will need to use an external, unknown DLL and build a java wrapper around it. 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. (I have no experience with C++ )

Following situation:

Let's say this DLL has a function, which contains one or more C++ classes as method signature. Then how could I call this function with JNI, as in my java project those custom classes in the DLL are non-existent? Is there a option to "clone" or "port" the C++ class to java? Is there anything I could do with a tool like Dependency Walker to resolve this problem?

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

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

/* 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
s.stkvc
  • 127
  • 1
  • 2
  • 12
  • 3
    How did you end up with this task in the first place? This is not a job for someone with no C++ experience. No, there isn't a way to clone/port the whole thing - not even if the .H file was available. DepWalk can help somewhat if you're lucky (e. g. the interface is not too hairy), but still, this will be an extremely manual job. To answer your question, the best approach would be to walk away from this task. – Seva Alekseyev Aug 14 '19 at 14:15
  • 1
    I'll add to the previous comment. Unless you know, and really know C++, doing JNI work is not going to go too well for you. The JNI is fragile as it is, and it takes someone with experience to know how to work around the fragilities. – PaulMcKenzie Aug 14 '19 at 14:22
  • What are the *exact* details of this "unknown DLL"? You need to know details before you can even tell if the task can be done or not. Because you simply can't write code that calls something that's "unknown". – Andrew Henle Aug 14 '19 at 14:27
  • @sstankov That's my entire point. Just what are you supposed to "wrap"? "Write a wrapper for this." You: "What does it do?" "Unknown." Do you even know if this "unknown DLL" is a [32- or 64-bit DLL?](https://stackoverflow.com/questions/2265023/load-32bit-dll-library-in-64bit-application) – Andrew Henle Aug 14 '19 at 14:44
  • 1
    There is another way. Find someone who has ample experience with both C++ and JNI (and, potentially, reverse engineering of compiled code), and pay them a ton of money. How important is this for the management, financially speaking? OBTW, C++ has no reflection. – Seva Alekseyev Aug 14 '19 at 14:59
  • @sstankov you can't call into a DLL if you don't know what its functions look like. There is no way to solve this without knowing the function declarations. Period. If you don't have the function declarations, the only way to get them is to reverse engineer the DLL itself, as there is no reflection in C++. – Remy Lebeau Aug 14 '19 at 18:46

1 Answers1

0

The best way here is to use 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.

You can find sample here: http://jnicookbook.owsiak.org/recipe-No-021/ and here 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/

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.

Oo.oO
  • 12,464
  • 3
  • 23
  • 45