过氏分享 http://blog.sciencenet.cn/u/xirongguo

博文

Java调用动态链接库(.dll)

已有 4505 次阅读 2011-6-17 19:55 |个人分类:系统工程|系统分类:科研笔记| java, Jnative

单个参数传递的情况,可以通过预先分配内存的方法获取函数返回值
public static boolean getXML(String[] args) throws NativeException, IllegalAccessException
    {
            JNative sat = null;
            try
            {
                String fun = "parse" + args[0];
                System.loadLibrary("ParseSat");
                JNative.setLoggingEnabled(false);
                sat = new JNative("ParseSat", fun);
                sat.setRetVal(Type.INT);
                sat.setParameter(0, args[1]);
                Pointer out = new Pointer(MemoryBlockFactory.createMemoryBlock(Integer.parseInt(args[2])));
                sat.setParameter(1, out.getPointer());
                sat.invoke();
                int res = sat.getRetValAsInt();
                args[3] = out.getAsString();
                out.dispose();
                if( res==1 )
                    return true;
                else
                    return false;
            }
            finally
            {
                if( sat!=null )
                    JNative.unLoadLibrary("ParseSat");
            }
    }
数组传递,主要通过对预先分配的内存格式化来实现参数的传入及返回值的取回
public LatLon[] getSatSwath(double sensor, double point, Date dt){
        JNative img = null;
        try
        {
            System.loadLibrary("SatSenPro");
            JNative.setLoggingEnabled(false);
            img = new JNative("SatSenPro", "getLatLon");
            img.setRetVal(Type.INT);           
            Pointer params = new Pointer(MemoryBlockFactory.createMemoryBlock(20*byteLen));
            params.zeroMemory();
            //initialize the out           
            int offSet = 0;
            offSet += this.iniParams(params, offSet, dt);
            offSet += params.setDoubleAt(offSet, sensor);
            offSet += params.setDoubleAt(offSet, point);
            offSet += params.setDoubleAt(offSet, 0);
            offSet += params.setDoubleAt(offSet, 0);
            offSet += params.setDoubleAt(offSet, 0);
            params.setDoubleAt(offSet, 0);
            img.setParameter(0, params.getPointer());
            img.invoke();
            int res = img.getRetValAsInt();           
            if(1==res){
                LatLon[] ll = new LatLon[2];
                ll[0] = LatLon.fromDegrees(params.getAsDouble(offSet-3*byteLen), params.getAsDouble(offSet-2*byteLen));
                ll[1] = LatLon.fromDegrees(params.getAsDouble(offSet-byteLen), params.getAsDouble(offSet));
                return ll;
            }
            else
                return null;
           
        }  catch (IllegalAccessException ex) {
            errMsg = ex.getMessage();
            return null;
        }  catch (NativeException ex) {
            errMsg = ex.getMessage();
            return null;
        }       
        finally
        {
            if( img!=null ){
                try {
                    JNative.unLoadLibrary("tranSat");
                } catch (NativeException ex) {
                    Logger.getLogger(SatTrackModel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }




https://blog.sciencenet.cn/blog-427394-456408.html

上一篇:WWJ B/S开发(二)
下一篇:Java Persistence(JPA)
收藏 IP: 124.16.186.*| 热度|

0

发表评论 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-20 15:25

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部