JAVA JNA 调用DLL示例参考

1.函数原型

MY_API  long __stdcall GapInfo(double inTemperature, double inPressure,double inFraction[],
    double* outDensity,double* outSpecificHeatV,double* outSpecificHeatP,
    double* outDynamicViscosity,double* outCompressfactor,double* outKinematicViscosity);


MY_API  bool __stdcall GetAPErrorInfo(long errID, char *buf, unsigned int bufLen);

 

2.下载jna.jar,加入引用库中,建立一个类文件

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.DoubleByReference;


public interface MyDll extends Library {
    public MyDll instanceDll = (MyDll)Native.loadLibrary("AP1700", MyDll.class);
    
    public NativeLong GapInfo(double inTemperature, double inPressure,double[] inFraction,
            DoubleByReference outDensity,DoubleByReference outSpecificHeatV,DoubleByReference outSpecificHeatP,
            DoubleByReference outDynamicViscosity,DoubleByReference outCompressfactor,DoubleByReference outKinematicViscosity);

    public boolean GetAPErrorInfo(NativeLong errID, byte[] buf, int bufLen);
}

 

3.调用类中的方法

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.DoubleByReference;

public class Demo {
    public static void main(String arg[])
    {
        System.setProperty("jna.encoding", "GBK");
        
        double    m_temperature = 100;
        double    m_pressure = 1;
        double    m_HEFraction = 1;
        double    m_N2Fraction = 0;
        double    m_CH4Fraction = 0;
        double    m_CO2Fraction = 0;
        double    m_H2OFraction = 0;
        
        double[] Fraction = new double[5];//组分数组
        Fraction[0]=m_HEFraction;//氦气
        Fraction[1]=m_N2Fraction;//氮气
        Fraction[2]=m_CH4Fraction;//甲烷
        Fraction[3]=m_CO2Fraction;//二氧化碳
        Fraction[4]=m_H2OFraction;////输出给用户的密度,定容比热,定压比热,粘度,压缩因子
        DoubleByReference Density = new DoubleByReference();
        DoubleByReference SpecificHeatV = new DoubleByReference();
        DoubleByReference SpecificHeatP = new DoubleByReference();
        DoubleByReference DynamicViscosity = new DoubleByReference();
        DoubleByReference Compressfactor = new DoubleByReference();
        DoubleByReference KinematicViscosity = new DoubleByReference();
          
        NativeLong errID = MyDll.instanceDll.GapInfo(m_temperature/*温度*/,m_pressure/*压力*/,Fraction/*组分*/,Density/*密度*/,SpecificHeatV/*定容比热*/,SpecificHeatP/*定压比热*/,DynamicViscosity/*动力粘度*/,Compressfactor/*压缩因子*/,KinematicViscosity/*运动粘度*/);
        if (errID.longValue() > 0)
        {
            byte[] buf = new byte[512];
            boolean b = MyDll.instanceDll.GetAPErrorInfo(errID, buf, buf.length);
            String bufStr = Native.toString(buf);
            System.out.print(bufStr);
            System.exit(0);
        }
        System.out.printf("密度:%.10f,\r\n定容比热:%.10f,\r\n定压比热:%.10f,\r\n动力粘度:%.10f,\r\n压缩因子:%.10f,\r\n运动粘度:%.10f",Density.getValue(),SpecificHeatV.getValue(),SpecificHeatP.getValue(),DynamicViscosity.getValue(),Compressfactor.getValue(), KinematicViscosity.getValue());
    }
}

 

4.说明

JNA调用DLL不需要编写C代码,节省很多时间,主要的难点是类型映射,这里没有演示数据结构和、回调函数和指针的调用,下次有时间会补上。

posted @ 2014-06-14 21:59  小D  阅读(8979)  评论(0编辑  收藏  举报