Java Native Interface - 在java中使用第三方dll文件
编辑问题:
尝试使用 JNA 从 Java 中使用 .dll 文件。我已经设法:
将 .dll 添加到系统库 - System.loadLibrary("NativeLibrary");
创建了一个 NativeInterface 来映射 .dll/.h 文件中的函数:
public interface NativeInterface extends Library, StdCallLibrary {
public int methodA(packageURL.NativeInterface.typeDefName n);
public int methodB();
public static class typeDefName implements Structure.ByReference{
public typeDefName(short s) {}
}
}
将映射添加到我的函数名称,因为它在 .dll 中的名称被“损坏” - 使用 Dependency Walker 发现了这一点
Map options = new HashMap();
options.
put(
Library.OPTION_FUNCTION_MAPPER,
new Mapper() {
public String getFunctionName(NativeLibrary library, Method method) {
return super.getFunctionName(library, method);
}
}
);
并且:
class Mapper implements FunctionMapper{
public String getFunctionName(NativeLibrary library, Method method) {
return "?" + method.getName() + "@@YAHPAEIPAPAXFPAUtypeDefName@@@Z";
}
}
现在(我不是当然)是如何创建一个 typeDefName 的对象来传递给方法 A
Edited Question:
Trying to use a .dll file from Java using JNA. I've managed to:
Add the .dll to the System Library - System.loadLibrary("NativeLibrary");
Created a a NativeInterface to map the functions in the .dll/.h file:
public interface NativeInterface extends Library, StdCallLibrary {
public int methodA(packageURL.NativeInterface.typeDefName n);
public int methodB();
public static class typeDefName implements Structure.ByReference{
public typeDefName(short s) {}
}
}
Added the mapping to my function name because its name in the .dll is "mangled" - Found this out using Dependency Walker
Map options = new HashMap();
options.
put(
Library.OPTION_FUNCTION_MAPPER,
new Mapper() {
public String getFunctionName(NativeLibrary library, Method method) {
return super.getFunctionName(library, method);
}
}
);
AND:
class Mapper implements FunctionMapper{
public String getFunctionName(NativeLibrary library, Method method) {
return "?" + method.getName() + "@@YAHPAEIPAPAXFPAUtypeDefName@@@Z";
}
}
Now (What I'm not sure of) Is how to create an Object of typeDefName to pass into Method A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标只是使用 Java 中的现有 DLL,则可以使用 JNA
根本不需要进行任何本机编程。 JNA动态访问第三方共享库的方法。您可以直接使用该库,也可以使用 JNA 完全用 Java 编写一个方便的包装器。
当您单击链接时,请务必向下滚动 - 有很多文档和内容,但页面顶部被文件列表占用,因此在向下滚动之前它看起来不太有用。
If your goal is just to use an existing DLL from Java, you can use JNA
without doing any native programming at all. JNA accesses the methods of third-party shared libraries dynamically. You could use the library directly or use JNA to write a convenient wrapper entirely in Java.
When you click the link, be sure to scroll down -- there's lots of documentation and stuff, but the top of the page is consumed by a file listing so it doesn't look very useful until you scroll down.
您可以在此处找到一本有关 JNI 的书,其中还包含一些代码示例。
但我建议首先学习 C 或 C++ 编程,尤其是指针的东西(因为你将需要它)。
创建 DLL 文件的方式取决于您使用的编译器,上述书籍中对此进行了描述...
You can find a book on JNI here, also containing some code examples.
But I would recommend learning to program in C or C++ first, especially the pointer stuff (as you will need it).
The way you create a DLL file depends on the compiler you use, it is described for some in the aforementioned book...