国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JNA調用C語言動態鏈接庫學習實踐總結

2019-11-11 06:05:00
字體:
來源:轉載
供稿:網友

非常感謝linux社區的zht666給我指了一條明路,本文轉載自: http://www.linuxidc.com/Linux/2014-04/99688p2.htm

2.JNA模擬普通傳值參數

C語言函數:

int function1(int a, float b, long c)

JNA模擬:

int function1(int a, float b, long c)

3.JNA模擬C語言數組

C語言函數:

void function1(char * data)

void function2(const unsigned char* data)

JNA模擬:

void function1(char[] data) 或者 void function1(byte[] data) void function2(char[] data) 或者 void function2(byte[] data)

4.JNA模擬基本類型指針

JNA的ByReference有很多子類,這些類都在com.sun.jna.ptr包中:

IntByReference,LongByReference,FloatByReference,DoubleByReference,ShortByReference、ByteByReference、PointerByReference等等

從這些名字大家應該可以看出來他們的作用。

下面直接上例子吧:

C語言函數:

long function(int * a, long * b, float * c, double * d, short * e)

JNA模擬:

long function(IntByReference aRef, LongByReference bRef, FloatByReference cRef, DoubleByReference dRef, ShortByReference eRef)

如何構建這些對象呢?

FloatByReference cRef = new FloatByReference(); //使用默認初始值(具體多少我也不知道) FloatByReference cRef = new FloatByReference(0); //初始值為0

調用方法和普通參數一樣:

function(…, cRef, …);

獲取結果值:

float fVal = cRef.getValue();

JNA都為我們做的很簡單,不是嗎?

5.JNA模擬指針、指針的指針、模擬void ,void 等指針*

C函數:

void function(int * pInt, int * ppInt, void pVoid, void ** ppVoid)

JNA模擬:

void function(IntByReference pInt, PointerByReference ppInt, Pointer pVoid, PointerByReference ppVoid) 調用舉例: IntByReference pInt = new IntByReference(0); PointerByReference ppInt = new PointerByReference(Pointer.NULL); //指向指針的指針,初始化為NULL Pointer pVoid = Pointer.NULL; //創建一個指向NULL的指針 PointerByReference ppVoid = new PointerByReference(Pointer.NULL); 調用:function(pInt, ppInt, pVoid, ppVoid);

(1)PointerByReference是指向指針的指針,遇到指針的指針都可以使用它來模擬,那么如何獲取到它指向的指針呢?

Pointer p = ppVoid.getValue(); //獲取指針

(2)如何獲取指針的指針呢?

Pointer p1 = ….; PointerByReference pp1 = new PointerByReference(p1); PointerByReference ppp1 = new PointerByReference(pp1.getPointer());

這些操作大家可以自己做實驗嘗試,對于PointerByReference對象,getValue()是取值,而getPointer()是取這個指針的指針。

看著復雜,其實都很簡單!JNA要比JNI好用多了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云安县| 宕昌县| 龙泉市| 安泽县| 夏河县| 盐山县| 通道| 阿巴嘎旗| 马关县| 宜兴市| 柯坪县| 竹山县| 阿克| 绥宁县| 陆川县| 萨嘎县| 大名县| 朔州市| 三都| 始兴县| 贵溪市| 建湖县| 清新县| 灵寿县| 沂南县| 香格里拉县| 通山县| 涟水县| 措美县| 哈巴河县| 临洮县| 海兴县| 清徐县| 东乌珠穆沁旗| 萨迦县| 信阳市| 永宁县| 中卫市| 绥化市| 江达县| 丰原市|