If you have mastered how to create a COM by ATL simple object,I think it 's a piece of cake for you to create double interfaces object (just select "dual" in your simple object property tab).and then you will see "interface ImyFun : IDispatch"--
the IDispatch starts to show for us magicaly.before this,we use IUnknown for our code,but now,we can use IDispatch to service our function need.
IDisPatch inherits from IUnknown interface,the essence of it is an address for special function need in memory.If you have learned <Principles of Computer Composition>,you must understand the way of memory visited indirectly.by the way,Just only decribes it from aspect of hardware to understand structure of memory and something else.Because we just write program based software platform,so "Pointer" is born from the world. From one Address to another Address to find some numbers that we need.If you have used pointer proficiently,I think the next question will get simple something for you.We haved talked about IDispathch as an interface as our quoting question,here,I wanna discuess about its details again.
Just look at the next table and you will see everything:

Inherit again and again,Is this clear?(IUnknown->IDispatch->ImyFun).But how to call interface function--- Add as following code:
void CTestDlg::OnTest()
{
::CoInitialize(NULL);
IDispatch *pTools = NULL;
CLSID myClass;
ZeroMemory(&myclass,sizeof(CLSID));
BSTR appid=::SysAllocString(L"DemoAdd.MyAdd");
::CLSIDFromProgID(appid,&myclass);
HRESULT hr=::CoCreateInstance (myclass,NULL,CLSCTX_INPROC_SERVER,IID_IDispatch,(void **)&pDisp);
BSTR names[1];
names[0]=::SysAllocString(L"sub");
DISPID result[1];
pDisp->GetIDsOfNames(IID_NULL,names,1,LOCALE_USER_DEFAULT,result);
VARIANT vp[2];
vp[0].vt=VT_I4;
vp[0].lVal=15;
vp[1].vt=VT_I4;
vp[1].lVal=13;
DISPPARAMS myparas;
myparas.cNamedArgs=0;
myparas.rgdispidNamedArgs=NULL;
myparas.rgvarg=vp;
myparas.cArgs=2;
VARIANT vResult;
vResult.vt=VT_I4;
EXCEPINFO ex;
hr=pDisp->Invoke(2,IID_NULL,LOCALE_USER_DEFAULT,
DISPATCH_METHOD,&myparas,&vResult,&ex,NULL);
pDisp->Release();
}
本文介绍如何使用ATL创建COM双接口对象,并详细解释了如何通过IDispatch接口来调用对象的方法。文章提供了具体的代码示例,演示了从初始化到释放资源的整个过程。
3773

被折叠的 条评论
为什么被折叠?



