Java Native Interface - 在java中使用第三方dll文件

发布于 2024-12-31 23:48:34 字数 1202 浏览 4 评论 0原文

编辑问题:

尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2025-01-07 23:48:34

如果您的目标只是使用 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.

万劫不复 2025-01-07 23:48:34

您可以在此处找到一本有关 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文