我有一个用Delphi编写的DLL(没有源代码,只有API)。 这是我试图通过Java使用JNA(版本5.4.0)调用的DLL函数

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9IcXhldC5wbmc=

以下显示了我的Java代码的外观。

public interface FPrintDLL extends StdCallLibrary {

FPrintDLL INSTANCE = Native.load("FPrintDLL", FPrintDLL.class);

int OPEN_TCPIP(WString ipAddress, int port, int deviceIndex, WString serialKey);

}

public static void main(String[] args) {

FPrintDLL fPrintDLL = FPrintDLL.INSTANCE;

WString ipAddress = new WString("192.170.1.3");

WString serialKey = new WString("12345678");

int deviceIndex = 4004;

int port = 9100;

int connectResult = fPrintDLL.OPEN_TCPIP(ipAddress, port, deviceIndex, serialKey); // Line 81

}

一切都很好,问题是我得到以下异常,

Exception in thread "main" java.lang.Error: Invalid memory access

at com.sun.jna.Native.invokeInt(Native Method)

at com.sun.jna.Function.invoke(Function.java:426)

at com.sun.jna.Function.invoke(Function.java:361)

at com.sun.jna.Library$Handler.invoke(Library.java:265)

at com.sun.proxy.$Proxy0.OPEN_TCPIP(Unknown Source)

at HelloJNA.main(HelloJNA.java:81)

根据异常日志,问题是由于int而发生的。 根据方法签名,它不使用任何指针/引用。 所以我不确定究竟是什么问题。

注 - 我设法在相同条件下成功运行此函数(OS,JAVA,DLL架构如下所列),使用下面的C#代码,

[DllImport(FPRINT_DLL, CallingConvention = CallingConvention.StdCall)]

public static extern int OPEN_TCPIP([MarshalAs(UnmanagedType.BStr)] string ipAddress,

int tcpPort,

int deviceIndex,

[MarshalAs(UnmanagedType.BStr)] string serialKey);

我之所以提到这是因为确认OPEN_TCPIP的给定API是正确的。 现在我想在Java和DLL之间进行直接交互。 (不使用某种包装类)

DLL是一个32位DLL,我在32位JVM,Windows10 64位上尝试它。 仅供参考我尝试了以下来源(列出的很少),但无法解决这个问题。

有谁知道如何解决这个问题?

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐