如何使用JNA访问Java代码中的DLL方法?
通过运行 System.loadLibrary("myAPI"),我验证了 DLL 文件“myAPI.dll”可以成功加载到我的 Eclipse Java 项目中。现在我需要从我的 Java 代码调用此 DLL 文件中指定的方法。为此,我将 JNA 添加到我的 Java 项目中。然后我编写了下面给出的代码片段,它应该能够获取类 IProject
和 ProjectFactory
的实例(在 DLL 文件中指定)。
我仍然不明白如何使用 JNA 正确实现这一点。我检查了不同的线程,例如这个,但是我检查的那些没有提供答案。非常感谢任何帮助。谢谢。
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");
}
}
}
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");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定您面临什么问题,但作为实践,您的 myAPI 接口应该使用适当的 参数逐字声明所有方法映射。我在你的界面中没有看到任何方法。
另请查看此链接正如@Perception上面提到的链接
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
如果这个 DLL 中没有隐藏 Java 类或 Java 源代码(这会……很奇怪),那么它永远不会以这种方式工作。您无法实例化 C# 类或使用 C# 接口。
MessageBox.Show(
也不是 Java,它是 Windows 窗体代码。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.