我在 C# 中创建了两个 dll 库,shuchu.dll 和 yinyong.dll
shuchu dll 的内容
namespace shuchu
{
public class Class1
{
[DllExport("getInfo", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static String GetInfo() {
return "A1B2C3";
}
}
}
yinyong dll 的内容
namespace yinyong
{
public class Ying
{
[DllExport("getShuChuInfo", CallingConvention = CallingConvention.Cdecl)]
public static string GetShuChuInfo()
{
return Class1.GetInfo();
}
[DllExport("getNormalInfo", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static string GetNormalInfo(string info)
{
return info;
}
}
}
yingyong.dll 中的 GetShuChuInfo 调用 GetInfo shuchu.dll不执行任何操作并返回调用方法的信息
他们都使用 offline.DllExport.1.7.4.29858 工具创建 ,DLL 都是 64 位的
这是我的 java 代码
public class NuoWenTest {
private static String basePath = "D:\\WorkSpace\\EclipseWorkSpace\\HC_Exmple\\NuoWen";
public interface NewtonsoftJsonDll extends Library {
NewtonsoftJsonDll INSTANCE = Native.load("shuchu.dll",NewtonsoftJsonDll.class, W32APIOptions.ASCII_OPTIONS);
String getInfo();
}
public interface Dll extends Library {
NewtonsoftJsonDll INSTANCE = NewtonsoftJsonDll.INSTANCE;
Dll dll = (Dll) Native.synchronizedLibrary(Native.load("yinyong.dll", Dll.class, W32APIOptions.ASCII_OPTIONS));
String getShuChuInfo();
String getNormalInfo(String info);
}
public static void main(String[] args) {
System.setProperty("jna.debug_load", "true");
System.setProperty("jna.debug_load.jna", "true");
System.setProperty("jna.library.path", basePath);
System.out.println(System.getProperty("os.arch"));
System.setProperty("jna.encoding", "GBK");
String value = "inputValue";
String normalInfo = Dll.dll.getNormalInfo(value);
System.out.println(normalInfo);
String shuChuInfo = Dll.dll.getShuChuInfo();
System.out.println(shuChuInfo);
}
}
执行结果
amd64
一月 10, 2025 1:32:31 下午 com.sun.jna.Native extractFromResourcePath
信息: Looking in classpath from sun.misc.Launcher$AppClassLoader@18b4aac2 for /com/sun/jna/win32-x86-64/jnidispatch.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.Native extractFromResourcePath
信息: Found library resource at jar:file:/D:/maven/localRepository/net/java/dev/jna/jna/5.12.1/jna-5.12.1.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.Native extractFromResourcePath
信息: Extracting library to C:\Users\PC\AppData\Local\Temp\jna-2547\jna7403058685325327933.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.Native loadNativeDispatchLibraryFromClasspath
信息: Trying C:\Users\PC\AppData\Local\Temp\jna-2547\jna7403058685325327933.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.Native loadNativeDispatchLibraryFromClasspath
信息: Found jnidispatch at C:\Users\PC\AppData\Local\Temp\jna-2547\jna7403058685325327933.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Looking for library 'shuchu.dll'
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Adding paths from jna.library.path: D:\WorkSpace\EclipseWorkSpace\HC_Exmple\NuoWen
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Trying D:\WorkSpace\EclipseWorkSpace\HC_Exmple\NuoWen\shuchu.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Found library 'shuchu.dll' at D:\WorkSpace\EclipseWorkSpace\HC_Exmple\NuoWen\shuchu.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Looking for library 'yinyong.dll'
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Adding paths from jna.library.path: D:\WorkSpace\EclipseWorkSpace\HC_Exmple\NuoWen
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Trying D:\WorkSpace\EclipseWorkSpace\HC_Exmple\NuoWen\yinyong.dll
一月 10, 2025 1:32:31 下午 com.sun.jna.NativeLibrary loadLibrary
信息: Found library 'yinyong.dll' at D:\WorkSpace\EclipseWorkSpace\HC_Exmple\NuoWen\yinyong.dll
inputValue
Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokePointer(Native Method)
at com.sun.jna.Function.invokePointer(Function.java:497)
at com.sun.jna.Function.invokeString(Function.java:660)
at com.sun.jna.Function.invoke(Function.java:434)
at com.sun.jna.Function.invoke(Function.java:361)
at com.sun.jna.Library$Handler.invoke(Library.java:265)
at com.sun.jna.Native$3.invoke(Native.java:1252)
at com.sun.proxy.$Proxy1.getShuChuInfo(Unknown Source)
at com.kaimingyun.iccard.controller.NuoWenTest.main(NuoWenTest.java:37)
如果调用 getNormalInfo,则不会报告错误,但调用了具有外部dll的方法getShuChuInfo()就会报错
java 版本为 1.8,JNA 版本为 5.12.1,使用 Windows 11,CPU i7-12700k,32GB RAM
我猜是依赖关系导致的问题 但不知道如何解决
我尝试将 getShuChuInfo() 方法的返回类型更改为 WString 和 Pointer,但它们都返回相同的错误。