如何访问静态方法?

时间:2013-12-20 17:36:03

标签: java jna

使用Java和JNA库,我想访问商业DLL文件(C#)中的静态方法。

不幸的是,Java 7不允许在接口中使用静态方法,尽管Java 8将对其进行修改,但最新的beta版本似乎无动于衷。

欢迎建议/更正(我是JNA的新手并且正在避免使用JNI),我使用Javonet来确认方法签名。

import com.sun.jna.Library;
import com.sun.jna.Native;

public class INeedHelp {
   public interface MyInterface extends Library {
       public static boolean isDisconnected(); //Mirror of C# method signature, but wont work
       public boolean isDisconnected(); //best fit, but throws exception "Error looking up function 'isDisconnected': The specified procedure could not be found."
   }

   public static void main(String[] args) {
       MyInterface anInstance = (MyInterface) Native.loadLibrary("theDLL", MyInterface.class);
       anInstance.isDisconnected();
   }
}

1 个答案:

答案 0 :(得分:1)

您应该可以使用Direct Mapping,例如;

import com.sun.jna.*;

public class MyClass {

    public static native boolean isDisconnected();

    static {
        Native.register("theDLL");
    }

    public static void main(String[] args) {
        MyClass.isDisconnected();
    }
}