Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNA Pointer retrieving value

Tags:

java

c

native

jna

I'm using JNA to access a dll and everything work fine ... while i'm in debug !!!

The problem is when i run my java code in non debug mode.

The purpose of the dll is to be called by passing somme parameters in a string and filling a char pointer with the result.

So to retrieve the result in java i'm using a PointerByReference object. When i'm in debug there's no problem , i have got the right result but there's only one charactere in my result in a standard running process.

This is my java call :

PointerByReference EXMES = new PointerByReference();
PointerByReference SCHAINE = new PointerByReference();
DoubleByReference dateDujour = new DoubleByReference(DATEJOUR);

log.debug(String.format("Appel avec les arguments : ECHAINE=[%s]; DATEJOUR=[%s]", echaine, sdf.format(dateEngagement)));

Map<String, Object> options = new HashMap<String, Object>();
options.put(Library.OPTION_TYPE_MAPPER, W32APITypeMapper.ASCII);

log.error(String.format("Default Charset : [%s]", Charset.defaultCharset().displayName()));
Native.setProtected(true);
MyNativeLibrary library = (MyNativeLibrary) Native.loadLibrary("myLib", MyNativeLibrary.class, options);
library = (MyNativeLibrary) Native.synchronizedLibrary(library);

String chaineAscii = new String("DATE_NAISSANCE\n19780102\nMEDIA\n4\n".getBytes(Charset.forName("US-ASCII")));

log.error(String.format("ECHAINE [%s]", chaineAscii));

library.SATINTS(chaineAscii, SCHAINE, dateDujour, EXMES);

String chaineSortie = new String(SCHAINE.getPointer().getString(0, false).getBytes(Charset.forName("US-ASCII")));
String chaineExmes = new String(EXMES.getPointer().getString(0, false).getBytes(Charset.forName("US-ASCII")));

log.debug(String.format("Retour taille Prexis : SCHAINE=[%d]; EXMES=[%d]", chaineSortie.length(), chaineExmes.length()));
log.debug(String.format("Retour Prexis : SCHAINE=[%s]; EXMES=[%s]", chaineSortie, chaineExmes));

This is the extract of my C function:

#define PRX_ALPHA char
#define EALPHA PRX_ALPHA *
#define SALPHA PRX_ALPHA *

EALPHA CHAINE;
SALPHA SCHAINE;
EDATE DATEJOUR;
SALPHA EXMES;

int winapi myFunction(
CHAINE,
SCHAINE,
DATEJOUR,
EXMES
) {


// Do something with the CHAINE and DATEJOUR then fill SCHAINE and EXMES with an answer
to my call

Thnaks in advance for every help, i'm stuck

like image 388
user799254 Avatar asked Jul 13 '26 00:07

user799254


1 Answers

PointerByReference is equivalent to void** in C. This does not match your native function prototype.

String is equivalent to const char*. Any changes made by your native code to the memory pointed to by that argument will be ignored. If you want to provide your native code a buffer to populate, use either byte[] or Memory.

Memory.getString(0) or Native.toString(byte[]) may then be used to construct a Java String from the result, instead of your rather verbose String constructor.

like image 91
technomage Avatar answered Jul 14 '26 13:07

technomage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!