How to access DLL methods in Java code using JNA?

2019-07-24 08:13发布

问题:

By running System.loadLibrary("myAPI"), I verified that the DLL file "myAPI.dll" can be successfully loaded into my Eclipse Java project. Now I need to call methods specified inside this DLL file from my Java code. To do this, I added JNA to my Java project. Then I wrote the below-given code snippet that should be able to get instances of classes IProject and ProjectFactory (specified in the DLL file).

I still don't understand how to properly implement this with JNA. I checked different threads, e.g. this one, but the ones I checked don't provide an answer. Any help is highly appreciated. Thanks.

import com.sun.jna.Library;
import com.sun.jna.Native;   

public class MyClass {

public interface myAPI extends Library {
    //...
}

void LoadProj() {
    myAPI api = (myAPI) Native.loadLibrary("myAPI",myAPI.class);
    String fileName = "xxx.sp";


    IProject project; // this is wrong but shows what I am trying to do
    try {
        project = ProjectFactory.LoadProject(fileName);
    }
    catch (Exception ex) {
        MessageBox.Show(this, ex.Message, "Load failure");
    }
}
}

回答1:

Not sure what problem you are facing but as a practice your myAPI interface should declare all the methods verbatim with appropriate parameter mapping. I don't see any methods inside your interface.

Please checkout the this link as well as the link mentioned above by @Perception



回答2:

If there are no Java classes or Java source hidden inside this DLL (which would be ... strange), then it will never work this way. You can't instantiate C# classes or use C# interfaces. MessageBox.Show( isn't Java either, it is Windows Forms code.



标签: java eclipse dll