How to access C ++ library method (DLL) from Java

I have a library written in C ++ (actually a Firefox plugin, xyz.dll), and I need to access its methods from Java.

public class AccessLibrary { public interface Kernel32 extends Library { public void showVersion(); } public static void main(String[] args) { Kernel32 lib = (Kernel32) Native.loadLibrary("xyz.dll", Kernel32.class); lib.showVersion(); } } 

The following error was received at runtime:

 java -jar dist/accessLibrary.jar Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'showVersion': The specified procedure could not be found. 

In the source code of the source library, the method is defined as follows

 void CPlugin::showVersion() { /* ... */ } 

I am very new to Java. Maybe I missed something basic. I looked at similar questions, but none of them solves my problem.

Forgot to mention that I use Windows 7 64bit and Java 7.

+4
source share
2 answers

First, you cannot export a class method and load it in java. The name will be distorted, and Java will not know how to properly name it. What you need to do is break it down into a separate function.

After that:

As already stated, make sure you export the function. You can export using one of two methods. The first is what is mentioned, which __declspec (dllexport) should use. The second is to put it in a def file.

Also, make sure you mark it as extern "C", otherwise the name will be malformed. All the details are here: Exporting functions from a DLL with dllexport

So, the signature should be something like this:

 extern "C" __declspec(dllexport) void showVersion () { } 

Finally, the addiction tool can be downloaded here: http://www.dependencywalker.com/

+2
source

I think your native library should provide a C-style interface like

 __declspec( dllexport ) void showVersion() { /* ... */ } 

Ideally, take a look at your DLL with depends.exe (which is available through the Windows SDK), there you will see if your DLL provides the correct export of functions.

+1
source

All Articles