拜讀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;
}
原文轉自:http://www.anti-gravitydesign.com