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