简体   繁体   English

如何使用JNA访问Java代码中的DLL方法?

[英]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. 通过运行System.loadLibrary("myAPI") ,我验证了DLL文件“ myAPI.dll”可以成功加载到我的Eclipse Java项目中。 Now I need to call methods specified inside this DLL file from my Java code. 现在,我需要从Java代码中调用此DLL文件中指定的方法。 To do this, I added JNA to my Java project. 为此,我将JNA添加到我的Java项目中。 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). 然后,我编写了下面给出的代码片段,该片段应能够获取IProjectProjectFactory类的实例(在DLL文件中指定)。

I still don't understand how to properly implement this with JNA. 我仍然不知道如何使用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 . 不知道您要面对什么问题,但是作为一种实践,您的myAPI接口应逐字声明具有适当参数映射的所有方法。 I don't see any methods inside your interface. 我在界面内看不到任何方法。

Please checkout the this link as well as the link mentioned above by @Perception 请检查此链接以及@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. 如果此DLL中没有隐藏Java类或Java源代码(这很奇怪),则它将永远无法以这种方式工作。 You can't instantiate C# classes or use C# interfaces. 您不能实例化C#类或使用C#接口。 MessageBox.Show( isn't Java either, it is Windows Forms code. MessageBox.Show(也不是Java,它是Windows窗体代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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