???????? 目錄
??????? com方式調用matlab(一)
??????? com方式調用matlab(二)?
??????? com方式調用matlab(三)
??????? com方式調用matlab(四)?
??????? com方式調用matlab(五)
??????? com方式調用matlab(六)
??????? com方式調用matlab(附:運行結果及代碼)
??????? 前面已經(jīng)介紹了幾個輔助類的實現(xiàn),比如讀取xml、實現(xiàn)用戶UI等等。那么如何使用這些輔助類呢,請看下面的類圖:
??????? 
??????? 負責與Matlab組件進行交互的類CMatlabGraph以及負責視圖顯示的CParamView都與配置類CGraphConfiguration相關聯(lián)。而CGraphConfiguration又通過CMyXml與配置文件打交道。另外CparamView與CEditCtrlFactory相關聯(lián),從而把配置信息顯示出來。
??????? 以下是CGraphConfiguration的類定義:
???????
class
?CGraphConfiguration??
{
public
:
????list
<
CString
>
?m_sGraphNames;
//
圖像名稱列表
????CString?m_sError;
//
錯誤信息
????CGraphConfiguration(CString?sFileName);
//
配置初始化
????
virtual
?
~
CGraphConfiguration();
????
bool
?FindGraphInfoByName(CMatlabGraph
*
?pGraph,CString?sName);
//
根據(jù)圖形名稱得到圖形信息
????
bool
?PutConfigurationToView(CParamView
*
?pView,CString?sName);
//
根據(jù)圖形名稱對控件列表初始
????
bool
?SetGraphParaValue(CParamView
*
?pView,CString?sName);
//
設置繪圖值
private
:
????CString?m_sFileName;
//
文件名稱
};
??????? 其中FindGraphInfoByName是用來被CMatlabGraph類的實例調用的。通過此方法,CMatlabGraph的實例取得了名如sNamr的圖形的配置信息。
??????? PutConfigurationToView用來將名字為sName的繪圖配置信息提供給CParamView的實例,后者在界面上繪制圖形名稱、參數(shù)等信息。
??????? SetGraphParaValue接受CParamView類對于配置信息的修改,并保存到配置文件中。
??????? 以下為方法FindGraphInfoByName的實現(xiàn):
bool?CGraphConfiguration::FindGraphInfoByName(CMatlabGraph*?pGraph,CString?sName)
{
????try
????{
????????//讀取配置文件
????????CMyXml?xml;
????????xml.LoadXmlFromFile(m_sFileName.GetBuffer(m_sFileName.GetLength()));
????????xml.GetMatchedRootElementList("Components");
????????xml.MoveCurrentTo(0);
????????xml.GetChildNodes();
????????for(int?iNode=0;iNode<xml.GetCurrentListLength();iNode++)
????????{
????????????if(!xml.MoveCurrentTo(iNode))
????????????????return?false;
????????????CString?sNodeName(_com_util::ConvertBSTRToString(xml.GetAttrbuteValue("Name")));
????????????//如果找到與當前名稱相同的文件則讀取所有配置內容
????????????if(sNodeName==sName)
????????????{
????????????????//取組件文件的名稱
????????????????CString?sComFileName(_com_util::ConvertBSTRToString(xml.GetAttrbuteValue("physicalName")));
????????????????//取GUID
????????????????xml.GetMatchedSubElementList("GUID");
????????????????xml.MoveCurrentTo(0);
????????????????CString?sGUID(_com_util::ConvertBSTRToString(xml.GetCurrentNodeValue()));
????????????????//取RIID
????????????????xml.GetMatchedParentElementList("RIID");
????????????????xml.MoveCurrentTo(0);
????????????????CString?sRIID(_com_util::ConvertBSTRToString(xml.GetCurrentNodeValue()));
????????????????//取方法名稱
????????????????xml.GetMatchedParentElementList("Method");
????????????????xml.MoveCurrentTo(0);
????????????????xml.GetMatchedSubElementList("MethodName");
????????????????xml.MoveCurrentTo(0);
????????????????CString?sMethod(_com_util::ConvertBSTRToString(xml.GetCurrentNodeValue()));
????????????????//將值傳遞給CMatlabGraph對象
????????????????USES_CONVERSION;
????????????????//賦組件文件名稱
????????????????pGraph->m_sComFileName=sComFileName;
????????????????//賦GUID
????????????????if(!SUCCEEDED(CLSIDFromString(T2OLE((LPCTSTR)sGUID.GetBuffer(sGUID.GetLength())),&(pGraph->m_clsid))))
????????????????{
????????????????????this->m_sError="取GUID失敗!請檢查配置文件";
????????????????????return?false;
????????????????}
????????????????//賦RIID
????????????????if(!SUCCEEDED(CLSIDFromString(T2OLE((LPCTSTR)sRIID.GetBuffer(sRIID.GetLength())),&(pGraph->m_riid))))
????????????????{
????????????????????this->m_sError="取RIID失敗!請檢查配置文件";
????????????????????return?false;
????????????????}
????????????????//賦方法名稱
????????????????pGraph->m_sMethodName=sMethod;
????????????????//取方法參數(shù)值并賦給圖形對象
????????????????//return?true;
????????????????if(!xml.GetMatchedParentElementList("Param"))
????????????????????return?true;
????????????????xml.MoveCurrentTo(0);
????????????????long?lParamCount=xml.GetCurrentListLength();
????????????????//對圖形對象的參數(shù)列表進行初始化
????????????????//因為有可能存在沒有參數(shù)的方法,所以這里在沒有取到任何參數(shù)的時候返回true
????????????????if(0==lParamCount)
????????????????????return?true;
????????????????//設置接口方法參數(shù)個數(shù)
????????????????pGraph->m_nparacount=lParamCount;
????????????????pGraph->m_pvars=new?CComVariant[lParamCount];
????????????????for(int?iNode=0;iNode<xml.GetCurrentListLength();iNode++)
????????????????{
????????????????????if(!xml.MoveCurrentTo(iNode))
????????????????????????return?false;
????????????????????//取得參數(shù)值的字符串形式
????????????????????CString?sValue(_com_util::ConvertBSTRToString(xml.GetCurrentNodeValue()));
????????????????????double?fValue;
????????????????????//轉換為浮點數(shù)
????????????????????sscanf(sValue,"%lf",&fValue);
????????????????????//轉換為VARIANT形式
????????????????????//pGraph->m_pvars[iNode].vt=
????????????????????pGraph->m_pvars[iNode]=CComVariant(fValue);
????????????????}
????????????????return?true;
????????????}
????????}
????????this->m_sError="配置信息未找到";
????????return?false;
????}
????catch(CException?*e)
????{
????????throw?e;
????}
}
??????? 以下為方法PutConfigurationToView的實現(xiàn)
bool?CGraphConfiguration::PutConfigurationToView(CParamView?*pView,CString?sName)//把xml的內容從界面顯示出來
{
????if(!pView)
????{
????????this->m_sError="視圖對象為空!";
????????return?false;
????}
????pView->m_sGraphName=sName;
????try
????{
????????//讀取配置文件
????????CMyXml?xml;
????????xml.LoadXmlFromFile(m_sFileName.GetBuffer(m_sFileName.GetLength()));
????????xml.GetMatchedRootElementList("Components");
????????xml.MoveCurrentTo(0);
????????xml.GetChildNodes();
????????for(int?iNode=0;iNode<xml.GetCurrentListLength();iNode++)
????????{
????????????if(!xml.MoveCurrentTo(iNode))
????????????????return?false;
????????????CString?sNodeName(_com_util::ConvertBSTRToString(xml.GetAttrbuteValue("Name")));
????????????//如果找到與當前名稱相同的文件則讀取所有配置內容
????????????if(sNodeName==sName)
????????????{
????????????????xml.GetMatchedSubElementList("Method");
????????????????xml.MoveCurrentTo(0);
????????????????xml.GetMatchedSubElementList("Param");
????????????????for(int?iNode=0;iNode<xml.GetCurrentListLength();iNode++)
????????????????{
????????????????????if(!xml.MoveCurrentTo(iNode))
????????????????????????return?false;
????????????????????//取得參數(shù)值的字符串形式
????????????????????CString?sStatic(_com_util::ConvertBSTRToString(xml.GetAttrbuteValue("Name")));
????????????????????//sStatic.Format("參數(shù)%d",iNode+1);
????????????????????CString?sValue(_com_util::ConvertBSTRToString(xml.GetCurrentNodeValue()));
????????????????????CString?sStaticText;
????????????????????sStaticText.Format("StaticParam%d",iNode);
????????????????????CString?sEditText;
????????????????????sEditText.Format("EditText%d",iNode);
????????????????????pView->m_editFactory->createCtrl(pView->m_pCWnd,"CStatic",sStaticText,CRect(30+120*iNode,50,70+120*iNode,70));//動態(tài)顯示參數(shù)名稱
????????????????????pView->m_editFactory->createCtrl(pView->m_pCWnd,"CEdit",sEditText,CRect(80+120*iNode,50,140+120*iNode,70));//動態(tài)顯示參數(shù)
????????????????????CWnd?*pWnd=(CWnd*)pView->m_editFactory->getCtrl(sStaticText);
????????????????????pWnd->SetWindowText(sStatic);//把參數(shù)名稱顯示出來
????????????????????pWnd=(CWnd*)pView->m_editFactory->getCtrl(sEditText);
????????????????????pWnd->SetWindowText(sValue);//把參數(shù)值顯示出來
????????????????}????????
????????????????return?true;
????????????}
????????}
????????return?true;
????}
????catch(CException?*e)
????{
????????//throw?e;
????????return?false;
????}
????
}
?
??????? 以下為SetGraphParaValue的實現(xiàn)
bool?CGraphConfiguration::SetGraphParaValue(CParamView*?pView,CString?sName)
{
????if(!pView)
????{
????????this->m_sError="視圖對象為空!";
????????return?false;
????}
????pView->m_sGraphName=sName;
????try
????{
????????//讀取配置文件
????????CMyXml?xml;
????????xml.LoadXmlFromFile(m_sFileName.GetBuffer(m_sFileName.GetLength()));
????????xml.GetMatchedRootElementList("Components");
????????xml.MoveCurrentTo(0);
????????xml.GetChildNodes();
????????for(int?iNode=0;iNode<xml.GetCurrentListLength();iNode++)
????????{
????????????if(!xml.MoveCurrentTo(iNode))
????????????????return?false;
????????????CString?sNodeName(_com_util::ConvertBSTRToString(xml.GetAttrbuteValue("Name")));
????????????//如果找到與當前名稱相同的文件則讀取所有配置內容
????????????if(sNodeName==sName)
????????????{
????????????????xml.GetMatchedSubElementList("Method");
????????????????xml.MoveCurrentTo(0);
????????????????xml.GetMatchedSubElementList("Param");
????????????????for(int?iNode=0;iNode<xml.GetCurrentListLength();iNode++)
????????????????{
????????????????????if(!xml.MoveCurrentTo(iNode))
????????????????????????return?false;
????????????????????CString?sValue;
????????????????????CString?sEditText;
????????????????????sEditText.Format("EditText%d",iNode);
????????????????????CWnd?*pWnd;
????????????????????pWnd=(CWnd*)pView->m_editFactory->getCtrl(sEditText);
????????????????????pWnd->GetWindowText(sValue);
????????????????????/*if(sValue==""||sValue==NULL)
????????????????????{
????????????????????????sValue="0.0";
????????????????????}*/
????????????????????xml.SetCurrentNodeValue(sValue.GetBuffer(sValue.GetLength()));
????????????????}
????????????????return?xml.Save();
????????????}
????????}
????????return?true;
????}
????catch(CException?*e)
????{
????????return?false;
????}
}
??????? 因為我們需要讓CGraphConfiguration能夠訪問到CMatlabGraph的成員,而這些成員又不希望被其他的類訪問到,所以這里把CGraphConfiguration定義為CMatlabGraph的友元類。
??????? 下面是CMatlabGraph的類定義:
class?CMatlabGraph??
{
public:
????CMatlabGraph(CGraphConfiguration?*cfg);
????virtual?~CMatlabGraph();
????bool?DrawGraph(CString?sGraphName);//繪圖
????CString?m_sError;//錯誤信息
private:
????CLSID?m_clsid;//組件對象ID
????IID?m_riid;//接口ID
????CString?m_sMethodName;//接口方法名稱
????CString?m_sComFileName;//組件文件名稱
????CComVariant?*m_pvars;//接口方法參數(shù)
????CGraphConfiguration?*m_pcfg;//配置
????int?m_nparacount;//接口方法參數(shù)個數(shù)
????friend?class?CGraphConfiguration;
????HINSTANCE?m_hinstLib;
};
????????其中,圖形繪制的工作要通過DrawGraph來進行。
??????? 具體實現(xiàn)為:
bool?CMatlabGraph::DrawGraph(CString?sGraphName)//繪圖
{
????if(!this->m_pcfg)
????{
????????this->m_sError="獲取配置文件失敗!";
????????return?false;
????}
????if(!m_pcfg->FindGraphInfoByName(this,sGraphName))
????{
????????this->m_sError=m_pcfg->m_sError;
????????return?false;
????}
????::CoInitialize(NULL);
????//分發(fā)接口,用于執(zhí)行不知道名字的接口方法
????IDispatch?*pIDisp=NULL;
????//指向讀入的組件文件的句柄
????HINSTANCE?hinstLib;
????//指向類廠的實例化方法
????MYPROC?ProcAdd;
????BOOL?fRunTimeLinkSuccess?=?FALSE;
????//int?rtn=0;
????//讀取組件文件載入內存
????hinstLib?=?LoadLibrary(this->m_sComFileName.GetBuffer(m_sComFileName.GetLength()));
????//如果執(zhí)行成功,則hinstLib非空
????if?(hinstLib?!=?NULL)?
????{
????????//讀取指向獲取類廠的函數(shù)的指針
????????ProcAdd?=(MYPROC)GetProcAddress(hinstLib,?"DllGetClassObject");
????????//?如果獲取成功,則創(chuàng)建類廠
????????if?(fRunTimeLinkSuccess?=?(ProcAdd?!=?NULL))
????????{
????????????//類廠接口
????????????IClassFactory?*pIf;
????????????//初始類廠接口為空
????????????pIf=NULL;
????????????if(SUCCEEDED(ProcAdd(m_clsid,IID_IClassFactory,(void?**)&pIf))&&(pIf!=NULL))
????????????{
????????????????if(!SUCCEEDED(pIf->CreateInstance(NULL,m_riid,(void?**)&pIDisp))||(pIDisp==NULL))
????????????????{
????????????????????????pIf->Release();
????????????????????????pIf=NULL;
????????????????????????FreeLibrary(hinstLib);
????????????????????????::CoUninitialize();
????????????????????????return?false;
????????????????}
????????????????m_hinstLib=hinstLib;
????????????????//因為已經(jīng)取得分發(fā)接口,故釋放類廠接口
????????????????pIf->Release();
????????????????pIf=NULL;
????????????????//根據(jù)名稱查找接口方法
????????????????USES_CONVERSION;
????????????????OLECHAR?FAR*?szMember?=?T2OLE((LPCTSTR)(this->m_sMethodName.GetBuffer(this->m_sMethodName.GetLength())));
????????????????//獲取方法ID
????????????????DISPID?MethodID;
????????????????if(!SUCCEEDED(pIDisp->GetIDsOfNames(IID_NULL,&szMember,1,LOCALE_SYSTEM_DEFAULT,&MethodID)))
????????????????{
????????????????????this->m_sError="取接口方法ID失敗";
????????????????????return?false;
????????????????}
????????????????//填入?yún)?shù)
????????????????DISPPARAMS?dispparams?=?{?this->m_pvars,?NULL,?this->m_nparacount,?0};
????????????????//調用方法
????????????????HRESULT?hr=pIDisp->Invoke(MethodID,IID_NULL,GetUserDefaultLCID(),DISPATCH_METHOD,&dispparams,NULL,NULL,NULL);
????????????????if(!SUCCEEDED(hr))
????????????????{
????????????????????this->m_sError.Format("調用失敗!錯誤碼:%x",hr);?//="調用失敗!";
????????????????????FreeLibrary(hinstLib);
????????????????????::CoUninitialize();
????????????????????return?false;
????????????????}
????????????????//釋放文件
????????????????//FreeLibrary(hinstLib);
????????????????//::CoUninitialize();
????????????????return?true;????
????????????}
????????}
????}
????::CoUninitialize();
????return?false;
}
??????? 這里的MYPROC是一個函數(shù)指針。當我們想要從一個dll庫里面調用一個函數(shù)的時候,首先需要定義一個與之匹配的函數(shù)指針。這里我們想要調用DLLGetClassObject這個函數(shù)以獲取類廠。所以預先定義一個與Dll中定義相同的函數(shù)指針:
???????
typedef?int?(CALLBACK?*MYPROC)(REFCLSID,REFIID,LPVOID?*);
??????? 在調用任何Dll庫中函數(shù)的時候,都需要首先通過LoadLibrary這個函數(shù)將Dll讀入內存并獲取句柄。該函數(shù)的參數(shù)很簡單,就是你要調用的那個Dll的文件名稱。
???????
hinstLib?=?LoadLibrary(this->m_sComFileName.GetBuffer(m_sComFileName.GetLength()));
??????? 之后,用函數(shù)GetProcAddress來取得Dll中的函數(shù),并用預先定義好的函數(shù)指針指向它。GetProcAddress的參數(shù)有兩個,一個是Dll在內存中的句柄,另外一個是你要調用的函數(shù)名稱。
ProcAdd?=(MYPROC)GetProcAddress(hinstLib,?"DllGetClassObject");
??????? 用DllGetClassObject來獲取類廠。我們的最終目的是獲取IDispatch,以此來通過組件所實現(xiàn)的接口方法名稱來呼叫方法,所以首先需要創(chuàng)建類廠,并用類廠的CreateInstance來獲取IDispatch。
if(SUCCEEDED(ProcAdd(m_clsid,IID_IClassFactory,(void?**)&pIf))&&(pIf!=NULL))
????????????{
????????????????if(!SUCCEEDED(pIf->CreateInstance(NULL,m_riid,(void?**)&pIDisp))||(pIDisp==NULL))
????????????????{

}
} ????????因為Matlab組件的接口對IDispatch進行了繼承,所以這里直接使用m_riid來獲取IDispatch接口。
??????? 然后使用IDispatch的GetIDsOfNames函數(shù)來獲得我們想要調用的那個方法。該方法以一個長整型MethodID來表示。
??????? 最后,把MethodID傳遞給IDispatch的Invoke方法,這樣就調用了Matlab組件中的繪圖方法。
???????
HRESULT?hr=pIDisp->Invoke(MethodID,IID_NULL,GetUserDefaultLCID(),DISPATCH_METHOD,&dispparams,NULL,NULL,NULL);
????????另外,該方法的參數(shù)被放到DISPPARAMS數(shù)組中:
???????
DISPPARAMS?dispparams?=?{?this->m_pvars,?NULL,?this->m_nparacount,?0};
?????????以上是繪圖操作的具體實現(xiàn),接下來看一下CParamView的代碼:
??????? 下面是CParamView的類定義:
class?CParamView
{
public:
????//控件工廠
????CEditCtrlFactory?*m_editFactory;
????//繪圖名稱
????CString?m_sGraphName;
????//窗口指針
????CWnd?*m_pCWnd;
????CParamView(CGraphConfiguration?*cfg,CWnd?*pCWnd);
????virtual?~CParamView();
????//視圖初始化
????bool?InitView(CString?sName);
????bool?SetParamValue(CString?sName);
private:
????CGraphConfiguration?*m_pcfg;//配置
};
??????? 其中InitView的作用是:當用戶變換所選圖像的時候,對圖像參數(shù)區(qū)進行刷新。以下為實現(xiàn):
???????
bool?CParamView::InitView(CString?sName)
{
????delete?this->m_editFactory;
????this->m_editFactory=new?CEditCtrlFactory();
????this->m_editFactory->createCtrl(this->m_pCWnd,"CStatic","",CRect(0,50,1024,70));
????return??this->m_pcfg->PutConfigurationToView(this,sName);
????
}
??????? 當用戶更改當前圖像的參數(shù)時,調用SetParamValue:
bool?CParamView::SetParamValue(CString?sName)
{
????return?this->m_pcfg->SetGraphParaValue(this,sName);
}
??????? 那么整個以Com方式調用Matlab的實現(xiàn)也就介紹完畢了。
??????? 從上面的代碼可以看出,CParamView以及CMatlabGraph與CGraphConfiguration的耦合是比較緊密的。另外在CGraphConfiguration中還存在不少duplication(副本)代碼。整體設計并不好,各個層次存在一定的依賴性。當然出現(xiàn)這些問題,也和當時時間緊迫有一定關系。這里的初衷還是整理Matlab組件的調用方式,給沒用過此方式的人提供若干思路。實在看不下去的同僚,也期待著您的批評指正。謝謝!
posted on 2006-08-28 21:41
littlegai 閱讀(895)
評論(0) 編輯 收藏 引用 所屬分類:
我的代碼玩具