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.