JNA throwing Invalid memory access

475 views
Skip to first unread message

Dewang Dave

unread,
Oct 26, 2021, 9:59:03 AM10/26/21
to Java Native Access
Hi,

I have a DLL called myDLL.dll and when checking the auto-generated testCFile.h file I see the following content -
-
//------------------------------------------------------------------------------------------------------------------------------------------
//! This module contains the __cdecl exported functions of myDLL.dll
//! The typedefs follow the following naming convention: PTR_[Name of the function]
//------------------------------------------------------------------------------------------------------------------------------------------

namespace N
{
    /// This is method1
    /// @param p1 some parameter
    /// @param p2 some parameter
    /// @param p3 some parameter
    /// @param p4 Boolean some parameter
    /// @return 0 if function execution is OK, 1 otherwise
    typedef int (*PTR_method1)(const char * p1, bool p2, const char * p3, bool& p4);

    /// This is method2
    /// @param pA some parameter
    /// @param pB some parameter
    /// @param pC some parameter
    /// @param pD some parameter
    /// @param pE some parameter value can be 'E1' or 'E2' only
    /// @param pF some parameter Can be eg. 'F1', 'F2' or 'F3'
    /// @param pG some parameter Can be eg. 'G1' or G2'
    /// @return 0 if function execution is OK, 1 otherwise
    typedef int (*PTR_method2)(const char * pA, const char * pB, const char * pC, const char * pD, const char * pE, const char * pF, const char * pG);
}

When running the following Java code on Windows machine -
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef;

public class Test {
     public interface CLibrary extends Library {
         CLibrary INSTANCE = Native.load("myDLL.dll", CLibrary.class);

        int method1(String input1, boolean input2, String input3, WinDef.BOOLByReference input4);
        int method2(String inputA, String inputB, String inputC, String inputD, String inputE, String inputF, String inputG);
     }

     public static void main(String[] args) {
        CLibrary library = CLibrary.INSTANCE;

        int result1 = library.method1("val1", true, "val3", new WinDef.BOOLByReference());
        System.out.println("result1: " + (result1 == 0));

        int result2 = library.method2("valA", "valB", "valC", "valD", "valE", "valF", "valG");
        System.out.println("result2: " + (result2 == 0));
     }
}

The output of the above code -
result1: true
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.method2(Unknown source)
        at Test.main(Test.java:19)

Any reason why?

Dewang Dave

unread,
Oct 26, 2021, 10:08:00 AM10/26/21
to Java Native Access
JNA version: jna-platform-5.9.0.jar
Windows version: Windows 10 Enterprise (64-bit OS, x64-based processor)
Java:
java version "11.0.12" 2021-07-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.12+8-LTS-237)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.12+8-LTS-237, mixed mode)

Matthias Bläsing

unread,
Oct 26, 2021, 11:35:49 AM10/26/21
to jna-...@googlegroups.com
Hi,

it would be helpful to see the _C_ header. But if I translate, there is
a declaration:

int method1(const char * p1, bool p2, const char* p3, bool& p4)

Now it depends how your bool type is defined. Is it an oldstyle bool?
Then it is a disguised int, if it is a newstyle bool, then it is 1
byte.

JNA by default maps java boolean to int and this you create an invalid
signature. You can check if

int method1(String input1, byte input2, String input3, ByteByReference input4);

helps.

Greetings

Matthias
> --
> 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 on the web visit
> https://groups.google.com/d/msgid/jna-users/2c0c75e0-a387-49ba-916d-25f5d57e95b3n%40googlegroups.com
> .


Dewang Dave

unread,
Oct 26, 2021, 11:42:28 AM10/26/21
to Java Native Access
Hi Matthias,

I believe the issue is occurring when I attempt to call the method2. If I comment out the code -
        //int result2 = library.method2("valA", "valB", "valC", "valD", "valE", "valF", "valG");
        //System.out.println("result2: " + (result2 == 0));

The code runs successfully without any error.

Thanks & Regards,
Dewang Dave

Matthias Bläsing

unread,
Oct 26, 2021, 12:25:51 PM10/26/21
to jna-...@googlegroups.com
Hi,

with your first call you invalidate the stack setup - from then on,
everything can break. At least try - I bet it cost you more time to
write this, then to test it.

Greetings

Matthias
> https://groups.google.com/d/msgid/jna-users/115f7aed-1970-4ac4-b546-4358176b307en%40googlegroups.com
> .


Dewang Dave

unread,
Oct 27, 2021, 4:51:14 AM10/27/21
to Java Native Access
Hi Matthias,

I am not sure if the issue is with the first call (i.e. method1) or not. So I removed the method1 from the interface as well as its call from the main method as below, but I am still getting the same error -
import com.sun.jna.Lirary;
import com.sun.jna.Native;

public class Test {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = Native.load("myDLL", CLibrary.class);

        int method2(String inputA, String inputB, String inputC, String inputD, String inputE, String inputF, String inputG);
    }

    public static void main(String[] args) {
        CLibrary library = CLibrary.INSTANCE;

        System.out.println("calling method2");
        int result2 = library.method2("valA", "valB", "valC", "valD", "valE", "valF", "valG");
        System.out.println("is method2 executed? " + (result2 == 0));
    }
}

Output is -
calling method2
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.method2(Unknown source)
        at Test.main(Test.java:15)

Thanks & Regards,
Dewang Dave

Matthias Bläsing

unread,
Oct 27, 2021, 3:06:59 PM10/27/21
to jna-...@googlegroups.com
Hi,

please provide the full source code for your tests. I still wondering
about the native signature, where you only provided, something that
does not match my definition of C header file.

The native code could fail and do strange things and we are looking for
answers where none can be found.

Greetings

Matthias
> https://groups.google.com/d/msgid/jna-users/fd88cfad-98c5-4dce-8c85-3da46cc954a3n%40googlegroups.com
> .


Dewang Dave

unread,
Oct 27, 2021, 3:15:02 PM10/27/21
to jna-...@googlegroups.com
Hi Matthias,

Due to security reasons, I can neither provide the DLL nor the C++/.H file contents.

Thanks & Regards,
Dewang Dave
You received this message because you are subscribed to a topic in the Google Groups "Java Native Access" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jna-users/FQeJSEucRDo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jna-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jna-users/8316c0c20eb1ef5c745f8d8968896eb3ca71c958.camel%40doppel-helix.eu.

Matthias Bläsing

unread,
Oct 27, 2021, 3:30:10 PM10/27/21
to jna-...@googlegroups.com
Hi,

Am Mittwoch, dem 27.10.2021 um 20:14 +0100 schrieb Dewang Dave:
> Hi Matthias,
>
> Due to security reasons, I can neither provide the DLL nor the C++/.H
> file contents.
>

sorry, in that case we have reached a dead-end. A final advise from my
work with the COM API: Try to find pure C samples and ensure, that they
are minimal and small. No try to rebuild the sample in Java and create
the necessary bindings on the way. That way you get a smoother learning
curve and ease into it.

Best luck

Matthias

Reply all
Reply to author
Forward
0 new messages