拜读zlyperson的轻松架起java连接com对象的桥梁后,受益匪浅,这里我想在zlyperson的基础上补充我在做一个项目中的一个运用方法。
项目需求:
从文件中读取数据,调用组件的算法来完成计算,将最终结果返回给java程序再实现结果的进一步运算和显示。
我所采取的策略是:
1、从文件中读取数据,如果在java中来完成的话,还要完成从java到com的数据传递,所以我索性在com中来完成,从java中传入一个文件的路径就行了。
activexcomponentactivexcom=newactivexcomponent("componentname.someclass");
dispatch.put(activexcom,"filepath",newvariant("e://数据"));
2、如何得到返还结果
publicstring[]getfinalresult()
{
varianttemp_var;
temp_var=dispatch.get(activexcom,"finalresults");
/*------------------------------------------------*/
//整个思路与mfc类似,先是转成safearray,然后得到其大小内容
/*------------------------------------------------*/
safearrayia=temp_var.tosafearray();
inttemp_llowerbound=ia.getlbound();
inttemp_lupperbound=ia.getubound();
inttemp_loptionalresultsnum=temp_lupperbound-temp_llowerbound+1;
stringtemp_sstring[]=newstring[temp_loptionalresultsnum];
for(inti=0;i<loptionalresultsnum;i++)
{
temp_sstring[i]=ia.getstring(i);
system.out.println(temp_sstring[i]);
}
returntemp_sstring;
}
闽公网安备 35060202000074号