天天看点

[转]jna模拟指针开辟空间,数组首地址获取值

http://blog.csdn.net/zhuzhichao0201/article/details/5817819

不是很明白,先记在这里

————————————————————————————————————————————————————————————

dll里面给的函数如下:

readwordblock(handle hscanner, 

byte epc_word, 

byte *idbuffer, 

byte mem, 

byte ptr, 

byte

len, 

byte *data, 

byte *accesspassword);

函数调用的时候byte

*data为输出参数,其他为输入参数。

c里面调用赋值为:

readwordblock(m_hscanner,epc_word,idtemp,mem,ptr,len,&db[0],accesspassword);

其中&db[0]为输出。

怎样在java里面实现呢?

在java模拟写入:

readwordblock(pointer hscanner, 

byte[]

idbuffer, 

byte len, 

bytebyreference data, 

byte[] accesspassword);

调用的时候:

bytebyreference p_data;

memory mymem = new memory(100);

p_data.setpointer(mymem);

readwordblock(m_hscanner,epc_word,idtemp,mem,ptr,len,p_db,accesspassword);

db = new byte[100];

mymem.read(0, db, 0, 100);

关于jna模拟指针的问题归纳:

byte* 可以模拟为bytebyreference, byte[],int[]等等,视应用时的类型而定。

如果使用int[]很有可能造成数据出错,因为int的数据长度与byte不一样。

而如果使用byte[]要考虑到数据超过127的时候会变为负数。

解决办法为先定义一个int型数组获取到值以后,再用byte[]型数组获取:

int[] idtemp[] = new int[12];

for (i = 0; i < 12; i++)

{

      idtemp[i] = idbuffer[cursel][i +

1];//将获取的值放在idtemp中

     }

byte[] tempidtemp = new byte[12];

for (int m = 0; m < 12; m++)

{             //将int型的值变为byte型的送进dll对应的byte型参数

      if

(idtemp[m] > 127)

       tempidtemp[m] = (byte) (idtemp[m]

- 256);

      } else

       tempidtemp[m] = (byte)

idtemp[m];

      }

}