Invalid Memory Access when using JNA to call a method in a C# DLL that depends on another DLL

48 views
Skip to first unread message

河神

unread,
Jan 13, 2025, 1:00:36 AMJan 13
to Java Native Access
Description

I am working on a Java project using JNA to call two C# DLLs ( and ). Here's the shuchu.dll ;  yinyong.dll

  • shuchu.dll contains the method .GetInfo()
  • yinyong.dll calls internally using its method .shuchu.dllGetShuChuInfo()

Calling from Java results in an Invalid memory access error. However, other methods in (like ) work perfectly.GetShuChuInfo()yinyong.dllgetNormalInfo()

Key Constraints:

  • I cannot modify the C# DLLs.
  • Both DLLs are 64-bit.
  • JNA is correctly configured in the project.

Environment
  • OS: Windows 11
  • CPU: Intel i7-12700K, 64-bit
  • Java Version: 1.8
  • JNA Version: 5.12.1
  • DLL Tool: UnmanagedExports (DllExport) 1.7.4.29

Code ExamplesC# DLL Code

shuchu.dll:

namespace shuchu {
    public class Class1 {
        [DllExport("getInfo", CallingConvention = 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 = CallingConvention.Cdecl)]
        public static string GetNormalInfo(string info) {
            return info;

        }
    }
}

Java Code

import com.sun.jna.*;

public interface Dll extends Library {
    Dll INSTANCE = Native.load("yinyong.dll", Dll.class);

    String getShuChuInfo();
    String getNormalInfo(String info);
}

public class Test {
    public static void main(String[] args) {
        System.setProperty("jna.library.path", "D:\\path\\to\\dlls");

        // success
        String normalResult = Dll.INSTANCE.getNormalInfo("Test Input");
        System.out.println("Normal Method Result: " + normalResult);

        // error
        String result = Dll.INSTANCE.getShuChuInfo(); // here
        System.out.println("Result: " + result);

    }
}

What I Have Tried
  1. Verified DLL Loading

    • Verified through logs and tools (e.g., Dependency Walker) that and are loaded successfully.shuchu.dllyinyong.dll
  2. Checked Return Types

    • Tried changing the return type of to and in Java, but the error persists.getShuChuInfo()PointerWString
  3. Examined Calling Conventions

    • Confirmed that both the DLLs and the JNA interface use .Cdecl
  4. Adjusted DLL Loading Order

    • Ensured that is loaded before , but it didn’t resolve the issue.shuchu.dllyinyong.dll
  5. Dependency Analysis

    • Used Dependency Walker to ensure that correctly references with no missing dependencies.yinyong.dllshuchu.dll

Error Details

Here’s the full stack trace of the error:

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 Test.main(Test.java:15)

What I Need Help With
  1. Identifying the root cause of the error.Invalid memory access
  2. Finding a solution or workaround to correctly call from Java.GetShuChuInfo()

Additional Notes
  • Both and are 64-bit and built using UnmanagedExports.shuchu.dllyinyong.dll
  • Other methods in that do not depend on work correctly.yinyong.dllshuchu.dll
  • I cannot modify the C# DLLs but can adjust the Java code or environment.

If needed, I can provide a minimal reproducible project for further debugging. Any help or guidance would be greatly appreciated! :-)

Matthias Bläsing

unread,
Jan 13, 2025, 11:51:36 AMJan 13
to jna-...@googlegroups.com
Hi,

this is just a gut feeling, but I see `string` in the C++ signature. To
my knowledge, that this might explain the issues. `string` is not plain
C, so will have destructors and such attached. So you might see a use-
after-free here, because from the native side the string can be
deallocated, while it is still in use on the java side.

This might shed some light:

https://stackoverflow.com/questions/20752001/passing-strings-from-c-sharp-to-c-dll-and-back-minimal-example/54563197

Greetings

Matthias

Am Sonntag, dem 12.01.2025 um 22:00 -0800 schrieb 河神:
> Description
> I am working on a Java project using JNA to call two C# DLLs ( and ). Here's the shuchu.dll ;  yinyong.dll
>  * shuchu.dll contains the method .GetInfo()
>  * yinyong.dll calls internally using its method .shuchu.dllGetShuChuInfo()
> Calling from Java results in an Invalid memory access error. However, other methods in (like ) work perfectly.GetShuChuInfo()yinyong.dllgetNormalInfo()
> Key Constraints:
>  * I cannot modify the C# DLLs.
>  * Both DLLs are 64-bit.
>  * JNA is correctly configured in the project.
> Environment
>  * OS: Windows 11
>  * CPU: Intel i7-12700K, 64-bit
>  * Java Version: 1.8
>  * JNA Version: 5.12.1
>  * DLL Tool: UnmanagedExports (DllExport) 1.7.4.29
> What I Have Tried
>    1. Verified DLL LoadingVerified through logs and tools (e.g., Dependency Walker) that and are loaded successfully.shuchu.dllyinyong.dll
>    2. Checked Return TypesTried changing the return type of to and in Java, but the error persists.getShuChuInfo()PointerWString
>    3. Examined Calling ConventionsConfirmed that both the DLLs and the JNA interface use .Cdecl
>    4. Adjusted DLL Loading OrderEnsured that is loaded before , but it didn’t resolve the issue.shuchu.dllyinyong.dll
>    5. Dependency AnalysisUsed Dependency Walker to ensure that correctly references with no missing dependencies.yinyong.dllshuchu.dll
> Error Details
> Here’s the full stack trace of the error:
> 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 Test.main(Test.java:15)
> What I Need Help With
>    1. Identifying the root cause of the error.Invalid memory access
>    2. Finding a solution or workaround to correctly call from Java.GetShuChuInfo()
> Additional Notes
>  * Both and are 64-bit and built using UnmanagedExports.shuchu.dllyinyong.dll
>  * Other methods in that do not depend on work correctly.yinyong.dllshuchu.dll
>  * I cannot modify the C# DLLs but can adjust the Java code or environment.
> If needed, I can provide a minimal reproducible project for further debugging. Any help or guidance would be greatly appreciated! :-)
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/jna-users/d2d1e041-400f-46b9-9c07-b3de92e8b1bdn%40googlegroups.com.

L Will Ahonen

unread,
Jan 13, 2025, 1:00:04 PMJan 13
to Java Native Access
Your c# code exports the functions as CallingConvention.Cdecl, you should specify the same calling convention on the Java side. If I remember correctly, JNA defaults to stdcall on Windows.

Try changing the java code from String getShuChuInfo() to Pointer getShuChuInfo(). If that fixes the crash, see what data the pointer contains, start by pointer.getByteArray( <a small number like 5 maybe> ) and see if the pointer does contain the character data you expect. If it crashes as soon as you try to read any data from the pointer, change it to long getShuChuInfo() and see if it returns a value that looks like a pointer.
Reply all
Reply to author
Forward
0 new messages