简体   繁体   English

JNA指针检索值

[英]JNA Pointer retrieving value

I'm using JNA to access a dll and everything work fine ... while i'm in debug !!! 我正在使用JNA访问一个DLL,一切正常......我正在调试!

The problem is when i run my java code in non debug mode. 问题是我在非调试模式下运行我的java代码。

The purpose of the dll is to be called by passing somme parameters in a string and filling a char pointer with the result. 通过在字符串中传递somme参数并使用结果填充char指针来调用dll的目的。

So to retrieve the result in java i'm using a PointerByReference object. 所以要在java中检索结果我正在使用PointerByReference对象。 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 : 这是我的java调用:

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: 这是我的C函数的摘录:

#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. PointerByReference等同于C中的void **。这与您的本机函数原型匹配。

String is equivalent to const char* . String等效于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 . 如果要为填充缓冲区提供本机代码,请使用byte[]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. 然后可以使用Memory.getString(0)Native.toString(byte[])从结果中构造Java String ,而不是使用相当冗长的String构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM