拜讀zlyperson的輕松架起Java連接COM對象的橋梁后,受益匪淺,這里我想在zlyperson的基礎上補充我在做一個項目中的一個運用方法。
項目需求:
從文件中讀取數(shù)據(jù),調(diào)用組件的算法來完成計算,將最終結(jié)果返回給java程序再實現(xiàn)結(jié)果的進一步運算和顯示。
我所采取的策略是:
1、從文件中讀取數(shù)據(jù),如果在java中來完成的話,還要完成從java到com的數(shù)據(jù)傳遞,所以我索性在com中來完成,從java中傳入一個文件的路徑就行了。
ActiveXComponentActiveXCom=newActiveXComponent("ComponentName.Someclass");
Dispatch.put(ActiveXCom,"FilePath",newVariant("E:\\數(shù)據(jù)"));
2、如何得到返還結(jié)果
publicString[]GetFinalResult()
{
Varianttemp_var;
temp_var=Dispatch.get(ActiveXCom,"FinalResults");
/*------------------------------------------------*/
//整個思路與MFC類似,先是轉(zhuǎn)成SafeArray,然后得到其大小內(nèi)容
/*------------------------------------------------*/
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 {
temp_sString[i]=ia.getString(i);
System.out.println(temp_sString[i]);
}
returntemp_sString;
}
項目需求:
從文件中讀取數(shù)據(jù),調(diào)用組件的算法來完成計算,將最終結(jié)果返回給java程序再實現(xiàn)結(jié)果的進一步運算和顯示。
我所采取的策略是:
1、從文件中讀取數(shù)據(jù),如果在java中來完成的話,還要完成從java到com的數(shù)據(jù)傳遞,所以我索性在com中來完成,從java中傳入一個文件的路徑就行了。
ActiveXComponentActiveXCom=newActiveXComponent("ComponentName.Someclass");
Dispatch.put(ActiveXCom,"FilePath",newVariant("E:\\數(shù)據(jù)"));
2、如何得到返還結(jié)果
publicString[]GetFinalResult()
{
Varianttemp_var;
temp_var=Dispatch.get(ActiveXCom,"FinalResults");
/*------------------------------------------------*/
//整個思路與MFC類似,先是轉(zhuǎn)成SafeArray,然后得到其大小內(nèi)容
/*------------------------------------------------*/
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
temp_sString[i]=ia.getString(i);
System.out.println(temp_sString[i]);
}
returntemp_sString;
}

