单个参数传递的情况,可以通过预先分配内存的方法获取函数返回值
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)