java 使用 JNA 链接到自定义 dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/492567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:38:33  来源:igfitidea点击:

Using JNA to link to custom dll

javadlljna

提问by luiscubal

how do I access custom .lib / .dll functions using JNA? Can someone provide an example?

如何使用 JNA 访问自定义 .lib / .dll 函数?有人可以提供一个例子吗?

Thank you.

谢谢你。

回答by luiscubal

Example (from Wikipedia):

示例(来自维基百科):

import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.Native;

/** Simple example of Windows native library declaration and usage. */
public class BeepExample {
   public interface Kernel32 extends StdCallLibrary {
       // FREQUENCY is expressed in hertz and ranges from 37 to 32767
       // DURATION is expressed in milliseconds
       public boolean Beep(int FREQUENCY, int DURATION);
       public void Sleep(int DURATION);
   }
   public static void main(String[] args) {
    Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", 
           Kernel32.class);
    lib.Beep(698, 500);
    lib.Sleep(500);
    lib.Beep(698, 500);
   }
}

In this case, we load it from the "kernel32.dll" library. I hope this makes JNA clearer.

在这种情况下,我们从“kernel32.dll”库加载它。我希望这能让 JNA 更清楚。

EDIT: I'll explain the code: You need to define an interface(that extends com.sun.jna.Library) with the functions you need from the library. Then, call com.sun.jna.Native.loadLibrary("LibraryName", InterfaceName.class). Finally, store the output in a variable with the type of the interface. Just call the functions from that variable.

编辑:我将解释代码:您需要使用库中所需的函数定义一个接口(扩展 com.sun.jna.Library)。然后,调用 com.sun.jna.Native.loadLibrary("LibraryName", InterfaceName.class)。最后,将输出存储在具有接口类型的变量中。只需从该变量调用函数。