Achelous
Achelous

Reputation: 1

Use JNA to call DLLS that have dependency concerns error message Invalid memory access

I created two dll libraries in C# , shuchu.dll AND yinyong.dll

The contents of shuchu dll

namespace shuchu
{
    public class Class1
    {

        [DllExport("getInfo", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
        public static String GetInfo() {
            return "A1B2C3";
        }
    }
}

The contents of 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;
        }
    }
}

Does GetShuChuInfo in yingyong.dll call GetInfo in shuchu.dll do nothing and return the information of the calling method

They all use offline. DllExport. 1.7.4.29 tools to create function ,DLLS are all 64-bit

This is my java code

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);

    }
}

Execution result

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)

No error is reported if getNormalInfo is called, only if a method exists that calls an external dll

java version is 1.8, JNA version is 5.12.1 using Windows 11, CPU i7-12700k, 32GB RAM

I guess it is because of the dependency relationship that causes the error, but I have read several articles to solve it without any effect.And the output information in the log has indicated that they were loaded normally

I have tried to change the return type of the getShuChuInfo() method to WString and Pointer, but they both return the same error.

Upvotes: 0

Views: 36

Answers (0)

Related Questions