如何使用JNA访问Java代码中的DLL方法? [英] How to access DLL methods in Java code using JNA?
问题描述
通过运行 System.loadLibrary(myAPI)
,我验证了DLL文件myAPI.dll可以成功加载到我的Eclipse Java项目中。现在我需要从我的Java代码调用此DLL文件中指定的方法。为此,我将JNA添加到我的Java项目中。然后我写了下面给出的代码片段,应该能够获取类 IProject
和 ProjectFactory
的实例DLL文件)。
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).
我仍然不明白如何使用JNA正确实现。我检查了不同的线程,例如这一个,但我检查的那些提供答案。任何帮助是非常感谢。谢谢。
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");
}
}
}
推荐答案
不知道你面临什么问题,但作为一个实践,你的myAPI界面应该用适当的参数映射。我的界面没有任何方法。
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.
请检查此链接以及上述由@Perception提及的链接
Please checkout the this link as well as the link mentioned above by @Perception
这篇关于如何使用JNA访问Java代码中的DLL方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!