JNA Pointer retrieving value
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 = (MyNativeLi开发者_StackOverflow社区brary) 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
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.
精彩评论