Calling dll implemented JNI from C++

Ragnarokkr Xia

Situation: I have a dll which implemented JNI,I want to call the methods in it from a Cpp application.

Current Status: According to my understanding, a dll which implemented JNI actually has nothing to do with JAVA, for example: In Test.java, I wrote
public native int Add(int a,int b);
And realized it with Cpp In TestDll.Cpp
JNIEXPORT jint JNICALL Java_SomeNamespace_Add(JNIEnv* _Env, jobject _Object, jint a, jint b) { return a+b; }
I think that such a program has nothing to do with JVM, the jint structure seems have been fully defined in jni.h.
So, I wonder if it's possible to call Java_SomeNamespace_Add directly without creating a VM from a Cpp application, if it do is possible,:

  1. What should the `JNIEnv*` and the `jobject` in the parameter list be?
  2. How can i convert a `jint` variable to a standard `int` variable?
  3. How can i convert a `jstring` variable to a standard `string` variable without using `_Env->FindClass("Ljava/lang/String;")` and a bunch of following code?
Carlo B.

For number 1 I think it would be a good idea to just create a simple alternative using a bit of refactoring.

Instead of putting the logic of the algorithm into the JNI call itself, move the logic to a separate c++ function and invoke that function from both use cases.

ie:

// MyJNIWrapper.cpp
#include "MyMathFunctions.h"
JNIEXPORT jint JNICALL Java_SomeNamespace_Add(JNIEnv* _Env, jobject _Object, jint a, jint b)
{
   return (jint)add((int)a, (int)b);
}

// MyMathFunctions.h
int add(int a, int b)
{
    return a + b;
}

// MyCppProgram.cpp
#include "MyMathFunctions.h"
int main()
{
    int c = add(a, b)
}

For number 2, you can convert between jint and int using standard C style casts:

int a = 10;
jint b = (jint) a;
int c = (int) b

As for number 3:

If you are calling the function from within JNI, you would use the pair of functions described here:

JNIEXPORT void JNICALLJava_MyJavaClass_printName(JNIEnv *env, jobject obj, jstring name)
{
    const char *str= (*env)->GetStringUTFChars(env,name,0);
    printf(“%s”, str);
    //need to release this string when done with it in order to
    //avoid memory leak
    (*env)->ReleaseStringUTFChars(env, name, str);
}

To convert from a C string like above to std::string you can do the following:

std::string cpp_string = str;

If you are not calling the function using JNI, it looks like you will need to try and roll your own conversion. I would see if you can use an existing VM implementation, figure out how they do it and mimic that.

For example you could try the Android VM with these as a starting point: https://android.googlesource.com/platform/dalvik/+/donut-release/vm/Jni.c#2230 https://android.googlesource.com/platform/dalvik.git/+/android-4.3_r3/vm/UtfString.cpp#284

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a DLL from an Applet via JNI

From Dev

Calling RenderScript from C / JNI

From Dev

Calling C system calls from JNI

From Dev

Calling C++ dll from python

From Java

Calling C++ dll from Java

From Dev

Build a dll in C++ for java which is calling another dll (JNI on Eclipse)

From Dev

JNI calling Java from C++ with multiple threads

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From

Calling Golang from Java using JNI on Windows leads to "A dynamic link library (DLL) initialization routine failed"

From Dev

Calling a function from a DLL which is developed in C++ from C

From Dev

Calling C++ DLL from C++ and C#

From Dev

FileNotFoundException when calling C# dll from C++/CLI

From Dev

Calling a C++ DLL from C# with unions

From Dev

Calling C++ dll Method from C#

From Dev

Crash when calling c++ dll function from c#

From Dev

in C# calling from dll a C function with both wchar and char

From Dev

Calling C dll from C#, return types slightly different

From Dev

Calling dll from a Win32 Console Application [C]

From Dev

Properly calling C++ functions in DLL from Delphi "Access Violation"

From Dev

Error when calling c++ dll from delphi

From Dev

Error while calling C DLL function from Python

From Dev

crash on "new" when calling a c++ dll from vb

From Dev

Calling a C# DLL from Delphi, with array of byte parameter

From Dev

C# WCF service implemented in a DLL

From Dev

Error while calling C# from java using jni4net from 64 bit Os

From Dev

Calling DLL from another namespace

From Dev

Calling function from dll with QT

From Dev

Calling a static void Java method from JNI

Related Related

  1. 1

    Calling a DLL from an Applet via JNI

  2. 2

    Calling RenderScript from C / JNI

  3. 3

    Calling C system calls from JNI

  4. 4

    Calling C++ dll from python

  5. 5

    Calling C++ dll from Java

  6. 6

    Build a dll in C++ for java which is calling another dll (JNI on Eclipse)

  7. 7

    JNI calling Java from C++ with multiple threads

  8. 8

    JNI: Calling a java method from C periodically is not working

  9. 9

    Java JNI NullPointerException after calling method from C with valid pointers

  10. 10

    Calling Golang from Java using JNI on Windows leads to "A dynamic link library (DLL) initialization routine failed"

  11. 11

    Calling a function from a DLL which is developed in C++ from C

  12. 12

    Calling C++ DLL from C++ and C#

  13. 13

    FileNotFoundException when calling C# dll from C++/CLI

  14. 14

    Calling a C++ DLL from C# with unions

  15. 15

    Calling C++ dll Method from C#

  16. 16

    Crash when calling c++ dll function from c#

  17. 17

    in C# calling from dll a C function with both wchar and char

  18. 18

    Calling C dll from C#, return types slightly different

  19. 19

    Calling dll from a Win32 Console Application [C]

  20. 20

    Properly calling C++ functions in DLL from Delphi "Access Violation"

  21. 21

    Error when calling c++ dll from delphi

  22. 22

    Error while calling C DLL function from Python

  23. 23

    crash on "new" when calling a c++ dll from vb

  24. 24

    Calling a C# DLL from Delphi, with array of byte parameter

  25. 25

    C# WCF service implemented in a DLL

  26. 26

    Error while calling C# from java using jni4net from 64 bit Os

  27. 27

    Calling DLL from another namespace

  28. 28

    Calling function from dll with QT

  29. 29

    Calling a static void Java method from JNI

HotTag

Archive