how to access a method of C++ library (DLL) from Java

前端 未结 2 676
野的像风
野的像风 2020-12-19 16:18

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

public class AccessLibrary {
    p         


        
2条回答
  •  甜味超标
    2020-12-19 16:43

    First, you cannot export a class method and load it into java. The name will get mangled, and java wouldn't know how to call it properly. What you need to do is break it out into a separate function on its own.

    After that:

    As already pointed out, make sure you export the function. You can export using one of two ways. The first is what is mentioned, which is to use __declspec( dllexport ). The second is to put it into the def file.

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

    So the the signature should be something like this:

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

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

提交回复
热议问题