JNA crashes when passing callback (INVALID MEMORY ACCESS) under windows

524 views Asked by At

I got the following code to embed a custom dll via JNA into our Java code:

public interface MyLibrary extends Library {
    MyLibrary INSTANCE = (MyLibrary) Native.loadLibrary("my_helper",  MyLibrary.class);

    interface render_t extends Callback {
        void callback(int id, int type, int dirty_rects_lenght, Pointer dirty_rects, Pointer buffer, int width, int height);
    }

    int init();
    int createApp(String param, render_t callback);
}

and this header file:

extern __declspec(dllexport) int __cdecl init(void);

struct rect_t {
    int x;
    int y;
    int width;
    int height;
};

typedef void (__cdecl *render_t) (int id, int type, int dirty_rects_lenght, void* dirty_rects, const void *buffer, int width, int height);
extern __declspec(dllexport) int __cdecl createApp(char *param, render_t render_callback);

The code works well under linux and osx. The DLL builds correctly with msvc and dumpbin also shows all exports. With JNA the init call is working but as soon as we call the callback function "createApp" it crashes with invalid_memory_access. I assume it is because of the calling conventions of the callback passed by JNA but I have no idea how to change this calling conventions. I have already tried to convert all __cdecl to __stdcall to use Windows calling-conventions and also changed JNA`s library and callback interfaces to stdcalllibrary and stdcallcallback respectively but it yields the same error.

OS: Windows 8.1 x64, Java: 8 x64, JNA: 4.1.0

Stacktrace:

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:383)
at com.sun.jna.Function.invoke(Function.java:315)
at com.sun.jna.Library$Handler.invoke(Library.java:212) <1 internal calls>
at mp.app.MyApp.<init>(MyApp.java:34)
0

There are 0 answers