天天看點

[轉]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];

      }

}