问题描述
如何使用JNA访问自定义.lib / .dll函数?
有人可以提供一个例子吗?
how do I access custom .lib / .dll functions using JNA?Can someone provide an example?
谢谢。
推荐答案
示例():
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);
}
}
在这种情况下,我们从kernel32 .dll库。
我希望这会使JNA更清晰。
In this case, we load it from the "kernel32.dll" library.I hope this makes JNA clearer.
编辑:我将解释代码:
你需要定义一个接口(扩展com。 sun.jna.Library)具有库中需要的功能。
然后,调用com.sun.jna.Native.loadLibrary(LibraryName,InterfaceName.class)。
最后,将输出存储在具有接口类型的变量中。
只需调用该变量的函数。
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.
这篇关于使用JNA链接到自定义dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!