Invalid Memory Access

206 views
Skip to first unread message

Watsons

unread,
Apr 4, 2016, 9:43:55 AM4/4/16
to Java Native Access
Hi,

sorry I am rather new to JNA.

I have the following line of codes.

#pragma once
#include "barcodeImage.h"

#define K120_HANDLE HANDLE

int WINAPI setImage(K120_HANDLE hnd, unsigned char *pImage, int len, int imgType);


I use JNAerator to generate the JNA Interface file, with some amendments.

//    public int setImage(Pointer hnd, ByteBuffer pImage, int len, int format);
//    public int setImagePointer hnd, ByteByReference pImage, int len, int format);
//    public NativeLong setImage(Pointer hnd, Pointer pImage, long len, long format);
//    public NativeLong setImage(Pointer hnd, byte[] pImage, long len, long format);
   
public int setImage(Pointer hnd, Pointer pImage, int len, int format);

I have tried various combinations of parameters by I am still getting the Invalid Memory Access.
java.lang.Error: Invalid memory access
    at com
.sun.jna.Native.invokeInt(Native Method)
    at com
.sun.jna.Function.invoke(Function.java:390)
    at com
.sun.jna.Function.invoke(Function.java:323)
    at com
.sun.jna.Library$Handler.invoke(Library.java:236)

Can someone kindly advise how I can resolved this error?


Thanks

L Will Ahonen

unread,
Apr 4, 2016, 3:16:16 PM4/4/16
to Java Native Access
Hi,

You need to post more source. One of your Pointers points to invalid data, how are they initialized in the sample code vs your Java code?

BR,
Will

Watsons

unread,
Apr 4, 2016, 11:01:14 PM4/4/16
to Java Native Access
Hi Will,
 
Sorry I am not sure what you mean by Pointers pointing to invalid data? Could you please elaborate?
 
As requested, here is how I called them.
 
PointerByReference ptrRef = new PointerByReference();
Pointer ptrImage;
byte byteImage[];

int nRet = TestJna.INSTANCE.initialise(ptrRef.getPointer());

byteImage
= fileToByte("sample.bmp");
ptrImage
= new Memory(byteImage.length);
ptrImage
.write(0, byteImage, 0, byteImage.length);

nRet
= TestJna.INSTANCE.setImage(ptrRef.getPointer(), ptrImage, byteImage.length, 1);

and the JNA interface file is
 
public interface TestJna extends StdCallLibrary {

   public static final TestJna INSTANCE = (TestJna) Native.loadLibrary("TestJna", TestJna.class);

   public int initialise(Pointer pQtf);

   public int setImage(Pointer hnd, Pointer pImage, int len, int format);

}
 

The first JNA call to initialise is ok. It always fails on the second JNA call to setImage.
 
 
Thanks

L Will Ahonen

unread,
Apr 5, 2016, 3:01:22 AM4/5/16
to Java Native Access
Hi,

Not sure how that is supposed to be called, as we're missing a crucial puzzle piece (the C code you're trying to adapt). From a "this is how things are often implemented" perspective, it would probably be

PointerByReference willReceiveHandle=new PointerByReference();
int nRet = TestJna.INSTANCE.initialise(willReceiveHandle); // Probably will write into your PointerByReference, that's how things usually go together
Pointer handleReceived=willReceiveHandle.value();

Pointer ptrImage;
byte byteImage[];
byteImage = fileToByte("sample.bmp"); 
ptrImage = new Memory(byteImage.length);
ptrImage.write(0, byteImage, 0, byteImage.length);

nret=TestJna.INSTANCE.setImage(handleReceived, ptrImage, byteImage.length, 1);

but you really need to post the C sample code so we can help you with this.

Will

Watsons

unread,
Apr 5, 2016, 8:01:53 AM4/5/16
to Java Native Access
Hi Will,
 
I am afraid I don't have any sample of how it should be called in C.
I only have the API document...
 
I hope this helps?
 
int WINAPI initialise(K120_HANDLE *hnd);

Description:
Initialize environment before any function call.

Arguments:
Name  I/O    Description
hnd   output handle

int WINAPI setImage(K120_HANDLE hnd, unsigned char *pImage, int len, int imgType
);

Description:

Input image which will be analysed.


Arguments:

Name        Description

hnd            handle returned by initialise funciton

pImage      image buffer pointer

len  image buffer length

imgType    image format

L Will Ahonen

unread,
Apr 6, 2016, 2:51:05 AM4/6/16
to Java Native Access
Hi,

From that api description it looks like my code below probably will work. Do you know what format the software is expecting for the image?

Will
Reply all
Reply to author
Forward
0 new messages