如何访问输出参数值

时间:2017-12-26 12:04:04

标签: java jna

这是我第一次使用JNA,我已经读过一些关于它的东西,还有一些东西对我来说不太清楚......

好吧,我需要使用来自第三方的.dll。他们提供了他们的库.h文件。

/*
 * Biblioteca de Automação Comercial
 *
 * API para facilitar integração com CTF Client
 */

/*prevent multiple includes*/
#ifndef _H_AUTTAR_BIBLIOTECA_AC_CTF
#define _H_AUTTAR_BIBLIOTECA_AC_CTF

/*useful definitions*/
#ifndef BOOL
  #define BOOL int
#endif

#ifndef TRUE
  #define TRUE 1
#endif

#ifndef FALSE
  #define FALSE 0
#endif

#ifndef NULL
  #ifdef __cplusplus
#define NULL 0
  #else
    #define NULL ((void *)0)
  #endif
#endif

/*OS specific*/
#ifdef _WIN32
  #define CTF_API  __stdcall
#else
  #define CTF_API  
#endif

/*make this code usable by C++*/
#ifdef __cplusplus 
  extern "C" {
#endif

/*API function calls*/
void CTF_API iniciaClientCTF(char* resultado,   /* out */
                             char* terminal,    /* in */
                             char* versao_ac,   /* in */
                             char* nome_ac,     /* in */
                             char* num_sites,   /* in */
                             char* lista_ips,   /* in */
                             char* criptografia,/* in */
                             char* log,         /* in */
                             char* interativo,  /* in */
                             char* parametros   /* in */
                             );

void CTF_API iniciaTransacaoCTF(char* resultado, /* out */
                                char* operacao,  /* in */
                                char* valor,     /* in */ 
                                char* num_doc,   /* in */ 
                                char* data_cli,  /* in */ 
                                char* num_trans  /* in */ 
                                );

void CTF_API iniciaTransacaoCTFext(char* resultado,  /* out */
                                   char* operacao,   /* in */
                                   char* valor,      /* in */
                                   char* num_doc,    /* in */
                                   char* data_cli,   /* in */
                                   char* num_trans,  /* in */
                                   char* dados);     /* in */

void CTF_API continuaTransacaoCTF(char* resultado, /* out */
                                  char* comando,   /* in/out */
                                  char* num_sc,    /* in/out */
                                  char* p_sc,      /* in/out */
                                  char* tam_sc,    /* in/out */
                                  char* aux);      /* in/out */

void CTF_API finalizaTransacaoCTF(char* resultado, /* out */
                                  char* confirmar, /* in */
                                  char* num_trans, /* in */
                                  char* data_cli );/* in */

/*make this code usable by C++*/
#ifdef __cplusplus 
    }
#endif

/*end of multiple-include prevention*/
#endif

我的映射界面是

import com.sun.jna.Library;
import com.sun.jna.Native;

public interface ICTFClient extends Library {

    ICTFClient INSTANCE = (ICTFClient) Native.loadLibrary("ctfclient", 
ICTFClient.class);

    public void iniciaClientCTF(byte[] resultado, 
                            byte[] terminal, 
                            byte[] versao_ac, 
                            byte[] nome_ac,
                            byte[] num_sites, 
                            byte[] lista_ips, 
                            byte[] criptografia, 
                            byte[] log, 
                            byte[] interativo,
                            byte[] parametros);

    public void iniciaTransacaoCTF(byte[]  resultado, 
                                byte[]  operacao, 
                                byte[]  valor, 
                                byte[]  num_doc,
                                byte[]  data_cli, 
                                byte[]  num_trans);

    public void iniciaTransacaoCTFext(byte[]  resultado, 
                                byte[]  operacao, 
                                byte[]  valor, 
                                byte[]  num_doc,
                                byte[]  data_cli, 
                                byte[]  num_trans,
                                byte[] dados);

    public void continuaTransacaoCTF(byte[]  resultado, 
                                byte[]  comando, 
                                byte[]  num_sc, 
                                byte[]  p_sc,
                                byte[]  tam_sc, 
                                byte[]  aux);


    public void finalizaTransacaoCTF(byte[]  resultado, 
                                byte[]  confirmar, 
                                byte[]  numTrans, 
                                byte[]  dataTransacao);
}

我的问题是:如何访问库返回的输出参数? 你认为我的界面映射是否正确?

1 个答案:

答案 0 :(得分:0)

" out"参数通常意味着函数将写入提供给它的指针。该函数似乎没有将输出缓冲区的大小作为参数,因此您需要阅读库的文档以找出输出需要多大的缓冲区(多少字节)。

byte[] outputBuf = new byte[4096]; // Change 4096 to the correct size
ICTFClient.INSTANCE.iniciaClientCTF(outputBuf, /* ... */);

// If the output is not ASCII, e.g. encoded in an encoding which supports Portugese characters, 
// pass the correct CharSet instead
String output = new String(outputBuf, StandarsCharsets.US_ASCII);
System.out.println("Output: " + output);

顺便说一下,如果所有char* in-params都是字符串,只需使用Java String - JNA就可以使用它。