1
votes

This is my first time using JNI. I need to call a third-party Visual C++ library from a Java program, so I wrote a wrapper in Visual C++. The library loads fine, but when I try to call a function from my wrapper library, I get the dreaded UnsatisfiedLinkError.

My wrapper includes the .h file created by javah, and has the following in the .cpp file:

JNIEXPORT jboolean JNICALL Java_Foo_bar (...)

but the library shows (in PE Explorer) _Java_Foo_bar@32

and my Java code has

public native boolean bar(...) inside class Foo

I suspect the @32 is what's tripping Java up, but I can't figure out how to get rid of it. I don't know much about compiler/linker settings and the like - I've for the most part been able to get away with defaults, or have had someone else handling that portion of a project.

Thanks in advance for any help!

1

1 Answers

3
votes

Try exporting your C++ function as a C function:

extern "C" JNIEXPORT jboolean JNICALL Java_Foo_bar (...)

This turns off name mangling.

Edit: this is indeed not mangling, but "decoration," which apparently is always done for the __stdcall convention that JNI uses. Adding a .def file or a /export linker argument seem to be the solution.