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代碼,節省很多時間,主要的難點是類型映射,這里沒有演示數據結構和、回調函數和指針的調用,下次有時間會補上。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM