简体   繁体   中英

How to access DLL methods in Java code using JNA?

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, eg 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");
    }
}
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM