2012-05-04 51 views
0
传递的字符串

我对JNA有点问题。通过参数

我的代码:

头C:

#ifndef IOPROTOCOL_H_INCLUDED 
#define IOPROTOCOL_H_INCLUDED 

typedef signed char INT8; 
typedef short  INT16; 
typedef int   INT32; 
typedef unsigned char UINT8; 
typedef unsigned short UINT16; 
typedef unsigned int UINT32; 

/* Types d'IO */ 
typedef enum { IOT_U1, IOT_U2, IOT_U8, IOT_I8, IOT_U16, IOT_I16, IOT_U32, IOT_I32, IOT_F32, IOT_STRING, IOT_MAX } IoTypeE; 

typedef union { 
    INT32 i; 
    UINT32 u; 
    float f; 
    char * s; 
} Value; 

typedef struct { 
    int (*Read) (char *rxbuf, int rxlen, UINT8 interTo); 
    int (*Write) (char *txbuf,int txlen); 
    int (*FunctionResponse) (int address, Value value, IoTypeE type); 
    int (*SendTraceRoute) (char * trace); 
} DeviceFuncT; 


    int readTrame(DeviceFuncT *deviceFunct, UINT32 function, UINT32 address, UINT32 countAddress, UINT32 slave, UINT32 type); 
    int writeTrame(DeviceFuncT *deviceFunct, UINT32 function, UINT32 address, Value value, UINT32 type, UINT32 slave); 


#endif // IOPROTOCOL_H_INCLUDED 

DeviceFuncT是用Java创建并在功能readTrame或writeTrame参数传递。我调用C(因为dll ^^)DeviceFuncT - >(* Read)(char * rxbuf,int rxlen,UINT8 interTo);

在java中,

public static interface ReadFunct extends com.sun.jna.Callback{ 
     int invoke(String rxbuf, int rxlen, byte interto); 
    } 

所以char* rxbuf == String rxbu˚F

我必须用Java处理RXBUF前完成功能后,我处理rxbux EN C,但我不到货具有值在Java中设置C中的函数...

你能帮助解决这个问题吗?请 :)。

对不起,我的说明。

谢谢。

回答

0

当您的本机类型为const char *(只读C字符串)时,您应该只使用String。如果您的本机类型实际上是本机端写入的缓冲区,则在从Java调用本机代码时需要使用原始数组com.sun.jna.Memory或NIO直接缓冲区,而在回调中从本机代码接收数据时需要使用NIO直接缓冲区。

由于您在回调中使用此参数,因此参数需要为Pointer。然后,您可以使用各种访问方法从内存读取或写入数据。

回调中不能使用原始数组或java.nio.Buffer,因为本机代码无法构造这些对象,而String是不合适的,因为您无法更改Java的内容String

0

这是没有必要改变参数在Java中,这通过一个字符串。你必须改变c中的代码来接收没有指针的字符串。

Int invoke (String rxbuf, int rxlen, int byte) { 
Char Newrxbuf [200]; 
    Strcpy (Newrxbuf, rxbuf.t_str()); 
} 

.t_str()适用于制造商C++ 2010,但可以在C

的其他版本字符串转换为 char *