JNA "Invalid memory access" with windows7 (64bit) and SiUSBXp.dll

1

Previously, I was using JNA (4.1.0) to access SiUSBXp.dll which was compiled for 32-bit systems, and it worked fine with the 32-bit JVM.

I recently upgraded my operating system to a 64-bit Windows 7, and I am now using a 64-bit JVM (jdk1.8.0_151) to run the same old Java program with the 64-bit version of the native DLL. However, I am having several problems when sending and receiving data with the library over USB.

  1. Sometimes the Java application gives an "Invalid memory access" exception

  2. Sometimes the functions return the wrong number, for example -134127606 instead of 3, when using IntByReference

I never had these issues on the 32-bit version. I tested the 64-bit DLL using a C++ application and it worked without any problems. Also, I've tried using JNA 4.5.0, but it still gives the "Invalid memory access" exception.

This is the C++ function which causes the problems:

SI_STATUS SI_Write (
    HANDLE Handle,
    LPVOID Buffer,
    DWORD NumBytesToWrite,
    DWORD *NumBytesWritten,
    OVERLAPPED *o = NULL
);

In Java, I've tried both of these declarations and they were both working well with the 32-bit version.

int SI_Write (HANDLE Handle, byte[] lpBuffer, int dwBytesToWrite, IntByReference lpdwBytesWritten);
int SI_Write (HANDLE Handle, Pointer lpBuffer, int dwBytesToWrite, IntByReference lpdwBytesWritten);

Example of calling this function in Java:

int     dwBytesWritten  = 0;
int     size            = 3;
byte[] buf_w= new byte[size];
IntByReference IBR_BytesWritten = new IntByReference();
IBR_BytesWritten.setValue(dwBytesWritten);
buf_w[0] = FT_WRITE_MSG;
buf_w[1] = (byte)(size & 0x000000FF);
buf_w[2] = (byte)((size & 0x0000FF00) >> 8);
Pointer ptr = new Memory(buf_w.length);
ptr.write(0, buf_w, 0, buf_w.length);
status = SiUSBXp.INSTANCE.SI_Write(dev_handle_ref.getValue(), ptr, size, IBR_BytesWritten);

This code working for few times then give Invalid memory access or return wrong value with IBR_BytesWritten.getValue()
Also, I tried with send byte[] directly instead of Pointer and give the same problem

Thanks in advance

Solution

The problem solved by using this signature

int SI_Write (HANDLE Handle, byte[] lpBuffer, int dwBytesToWrite, IntByReference lpdwBytesWritten, Pointer overlapped);

I call that function with NULL as final parameter, which would pass the null pointer to the function. Even it take default value in C library = NULL

java
c++
dll
usb
jna
asked on Stack Overflow Jan 2, 2018 by Ghazal • edited Feb 5, 2018 by Ghazal

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0