最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java使用JNA(Java Native Access)調(diào)用dll的方法

 更新時(shí)間:2013年11月11日 11:29:27   作者:  
java使用JNA(Java Native Access)調(diào)用windows系統(tǒng)的dll文件的例子

JNA(Java Native Access):建立在JNI之上的Java開源框架,SUN主導(dǎo)開發(fā),用來(lái)調(diào)用C、C++代碼,尤其是底層庫(kù)文件(windows中叫dll文件,linux下是so【shared object】文件)。
JNI是Java調(diào)用原生函數(shù)的唯一機(jī)制,JNA就是建立在JNI之上,JNA簡(jiǎn)化了Java調(diào)用原生函數(shù)的過(guò)程。JNA提供了一個(gè)動(dòng)態(tài)的C語(yǔ)言編寫的轉(zhuǎn)發(fā)器(實(shí)際上也是一個(gè)動(dòng)態(tài)鏈接庫(kù),在Linux-i386中文件名是:libjnidispatch.so)可以自動(dòng)實(shí)現(xiàn)Java與C之間的數(shù)據(jù)類型映射。從性能上會(huì)比JNI技術(shù)調(diào)用動(dòng)態(tài)鏈接庫(kù)要低。
1.簡(jiǎn)單寫個(gè)windows下的dll,文件命名為forjava.dll,其中一個(gè)add函數(shù),采用stdcall調(diào)用約定

復(fù)制代碼 代碼如下:

main.h文件
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport) __stdcall
#else
    #define DLL_EXPORT __declspec(dllimport) __stdcall
#endif

#ifdef __cplusplus
extern "C"
{
#endif

int DLL_EXPORT add(int a,int b);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

main.cpp

#include "main.h"

// a sample exported function
int DLL_EXPORT add(int a ,int b)
{
    return a+b;
}

extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}
 


2.將jna.jar導(dǎo)入eclipse工程中,java代碼如下
復(fù)制代碼 代碼如下:

//import com.sun.jna.Library; cdecl call調(diào)用約定
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.win32.StdCallLibrary;

public class main {

    public interface CLibrary extends StdCallLibrary { //cdecl call調(diào)用約定時(shí)為L(zhǎng)ibrary
        CLibrary INSTANCE = (CLibrary)Native.loadLibrary("forjava",CLibrary.class);
        public int add(int a,int b);
    }

    public static void main(String[] args) {
        System.out.print(CLibrary.INSTANCE.add(2,3));
    }
}
 

相關(guān)文章

最新評(píng)論

普陀区| 锡林郭勒盟| 如东县| 油尖旺区| 贵溪市| 海原县| 牟定县| 松桃| 大丰市| 阿尔山市| 新绛县| 综艺| 聂荣县| 汝城县| 德安县| 黄大仙区| 高州市| 望奎县| 海淀区| 聂荣县| 含山县| 平凉市| 兴安县| 托克托县| 盐边县| 肥乡县| 孝昌县| 贺兰县| 宜章县| 浦江县| 繁峙县| 瑞金市| 萨嘎县| 广宗县| 仙游县| 涟源市| 洛浦县| 繁峙县| 孙吴县| 崇阳县| 富顺县|