簡體   English   中英

Java本機接口-在Java中使用第三方dll文件

[英]Java Native Interface - Using a third party dll file in java

編輯問題:

嘗試使用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中

如果您的目標只是使用Java中的現有DLL,則可以使用JNA,而無需進行任何本機編程。 JNA動態訪問第三方共享庫的方法。 您可以直接使用該庫,也可以使用JNA完全用Java編寫方便的包裝器。

當您單擊鏈接時,請確保向下滾動-有很多文檔和資料,但是頁面頂部被文件列表占用,因此直到您向下滾動才顯得很有用。

您可以在此處找到有關JNI的書,其中還包含一些代碼示例。

但是我建議您首先學習使用C或C ++進行編程,尤其是指針(根據需要)。

創建DLL文件的方式取決於您使用的編譯器,在前面的書中對此進行了介紹...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM