JNA Invalid memory access while writing to file

For debugging reasons, I am writing a jna wrapper for the C ++ DLL (compiled with gcc and mingw32)

write16Byte.dll

void write16Byte(const BYTE* mem) {
  FILE* out = fopen("BSTRvalues.txt", "a+");
  if (out == NULL) {
    printf("Error opening file!\n");
  return;
  }
  for (int i=0; i<16; i++) fprintf(out, "0x%x ", mem[i]);
  fwprintf(out, L"\n");
  fclose(out);
}

jna wrapper

public interface W16BDll extends com.sun.jna.Library {
  W16BDll INSTANCE = (W16BDll)com.sun.jna.Native.loadLibrary("write16Byte.dll", W16BDll.class);
  void write16Byte(com.sun.jna.Memory version);
}

Calling fprintf leads to "java.lang.Error: Invalid memory access" because when I delete fprintf everything works fine (I already read the thread in JNA Invalid memory access when writing to stdout )

+4
source share
1 answer

If you include warnings in your compiler ( -Wallin gcc), it will tell you that you disagree with your format string and the actual argument.

"%x" int; const BYTE. , , , .

mem[i] int ( , const BYTE).

0

All Articles