Can I mix JNA with JNI

I have a custom dll that I am accessing with Java using JNA. So far, everything is working fine. Now, however, I would like to create Java classes from my C code. I assume that this cannot be done using JNA, so I created a JNI method, but that leads me to an UnsatisfiedLinkError. So my question is: can I mix JNA and JNI when accessing the same DLL, and if so, how do I do it?

+3
source share
2 answers

Of course, you can mix access to the DLL, since it loads once only anyway. The problem is how the link to your application works:

JNA:

JNA jna, - DLL. , DLL, .

JNI:

Java , DLL. com.company.SomeClass, int doStuff(int i, long john) :

JNIEXPORT jint JNICALL Java_SomeClass_doStuff(JNIEnv *env, jint i, jlong john) {
    return ...whatever...
}

, UnsatisfiedLinkException.

:

, DLL, , -, . , javah.

Java 2 - 5: JNI.

+4

. extern "C" JNIEXPORT, JNA.

:

// Example DLL header file MyDLL.dll
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

extern "C" {
   MYDLL_API void HelloWorld(void);    
}

extern "C" {
   JNIEXPORT void JNICALL Java_MyJavaMain_HelloWorld(void); 
}

//Example CPP file MyDLL.cpp
#include "MyDLL.h"
#include "stdio.h"

extern "C" declspec(dllexport)

void HelloWorld(void){
    printf("Hello World From Dll");
}
0

Source: https://habr.com/ru/post/1739465/


All Articles