Using JNA to link to custom dll

 ̄綄美尐妖づ 提交于 2019-12-28 12:31:09

问题


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

Thank you.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/492567/using-jna-to-link-to-custom-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!