开发者

Java app able to load library but can't find function (visual c++ name mangling?)

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.

Th开发者_如何转开发anks in advance for any help!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜