How to implement double interfaces by IDispatch automatically basing MFC

本文介绍如何使用ATL创建COM双接口对象,并详细解释了如何通过IDispatch接口来调用对象的方法。文章提供了具体的代码示例,演示了从初始化到释放资源的整个过程。

     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();  

 

   }

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值