【问题标题】:Using JNA to link to custom dll使用 JNA 链接到自定义 dll
【发布时间】:2009-01-29 17:20:46
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: java dll jna


    【解决方案1】:

    示例 (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);
       }
    }
    

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

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

    【讨论】:

    • 您应该为您给出的具体示例扩展 StdCallLibrary。它是否适用于用户的自定义库取决于该库使用的调用约定。
    • 如何为我的自定义 dll 提供路径我的意思是如果我的 dll 位于 e:/Mydll 中的某个位置。那么如何提供路径,如果 Mydll 使用其他一些 dll 和库制作,那么我是否还需要在某处为这些 dll 提供路径?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多