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

前端 未结 2 662
野的像风
野的像风 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/

    0 讨论(0)
  • 2020-12-19 16:46

    I think your native library needs to provide a C-style interface, for example

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

    Ideally, take a look at your DLL with depends.exe (which is available through the Windows SDK), there you'll see if your DLL provides the correct function exports.

    0 讨论(0)
提交回复
热议问题