A sample for COM Event

 

1.       COM Server

Create a COM project using ATL, such as ChildSvr.

Add CoClass (Child) with Connect point.(The IDE will add a CProxy_IChildEvents for you)

Add method to interface.

    Note: In this project, the _IChildEvents is not implemented by this project.

2.       Use this CoClass in VB

   Dim WithEvents objChild As ChildSvrLib.Child 

   'Define a sub to handle the Event:

   Private Sub objChild_AfterCry() Handles objChild.AfterCried

 

 

 

 

 

        MsgBox("I have handled the cring child!!!")

 

 

 

 

 

End Sub

3.       Use this CoClass in VC

 

A.      Create a ATL project for implement the outgoing interface.( _IChildEvents).

Create a project named “ChildSvrEventATL

 

 

 

 

 

Add a CoClass (dual interface).

 

 

 

 

 

Implement the interface by derived from the IDispEventSimpleImp.

 

 

 

 

 

Such as:

 

 

 

 

 

public IDispEventSimpleImpl<1, CChildSvrT, &__uuidof(_IChildEvents)>

 

 

 

 

 

define the function information for use in CPP file

     

_ATL_FUNC_INFO infoComputationComplete =

 

 

 

 

 

{CC_STDCALL, VT_I4, 0, {} };

 

 

 

 

 

        

 

 

 

 

 

        Extern the variable:

 

 

 

 

 

       extern _ATL_FUNC_INFO infoComputationComplete;

 

 

 

 

 

      

 

 

 

 

 

    BEGIN_SINK_MAP(CChildSvrT)

 

 

 

 

 

           SINK_ENTRY_INFO(1, __uuidof(_IChildEvents), 1, AfterCried, &infoComputationComplete)

 

 

 

 

 

    END_SINK_MAP()

 

 

 

 

 

Add a variable in the coclass

 

 

 

 

 

   CComQIPtr<IChild> m_spChild

 

   Add two function for IChildSvrT :

 

 

 

 

 

    STDMETHODIMP CChildSvrT::AttachEvents(IDispatch* pSvr)

 

 

 

 

 

    {

 

 

 

 

 

    // TODO: Add your implementation code here

 

 

 

 

 

//  AFX_MANAGE_STATE(AfxGetAppModuleState());

 

 

 

 

 

    HRESULT hr = S_OK

    if(NULL != pSvr

   

       m_spChild = pSvr

       hr = IDispEventSimpleImpl<1, CChildSvrT, &__uuidof(_IChildEvents)>::

 

 

 

 

 

          DispEventAdvise(pSvr);

 

 

 

 

 

   

  return S_OK;

 

 

 

 

 

 

 

STDMETHODIMP CChildSvrT::DetachEvents(void)

 

 

 

 

 

{

 

 

 

 

 

    // TODO: Add your implementation code here

 

 

 

 

 

//  AFX_MANAGE_STATE(AfxGetAppModuleState());

 

 

 

 

 

    HRESULT hr = S_OK;

 

 

 

 

 

   

 

 

 

 

 

    /*hr = AtlUnadvise(m_spChild, __uuidof(_IChildEvents), dwCookie);*/

 

 

 

 

 

    hr = IDispEventSimpleImpl<1, CChildSvrT, &__uuidof(_IChildEvents)>::

 

 

 

 

 

       DispEventUnadvise(m_spChild);

 

    return S_OK;

 

 

 

 

 

}

 

 

 

 

 

B.    Create the client

 

 

 

 

 

Create a win32 console project

 

 

 

 

 

Init the com

 

 

 

 

 

Create two coclass object

 

 

 

 

 

Attach Events

 

 

 

 

 

Use  the Child Object

     

      Detach Event

 

 

 

 

 

End.

 

 

 

 

 

     

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MFC中实现 EventSink 。 (1) 在MFC中,添加ATL简单对象 CFileMonitorSink (2) 添加继承父类 IDispEventImpl public IDispEventImpl (1) 0 唯一标识符, 用于区别 连接到 事件源的多个客户端 CFileMonitorSink, 当前类名 _IFun1Events, COM 中的事件源接口, 包含各种事件 __ATLEventLib, COM 中Lib类 具体查 MSDN --IDispEventImpl (2) 添加映射项 BEGIN_SINK_MAP(CFileMonitorSink) SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 1, OnNotify) //0 唯一标识符,用于区别 连接到 事件源的多个客户端 同上 , 1, 事件号 , 发生1号事件 由OnNotify来处理 SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 2, OnNotify2) //发生2号事件 由OnNotify2来处理 END_SINK_MAP() 并添加方法 STDMETHOD(OnNotify)(void); //事件处理类 STDMETHOD(OnNotify2)(CHAR* lszContent); (3) 连接到COM中的事件容器 添加变量 CComPtr m_Object; //COM 中的事件源对象 添加方法 STDMETHOD(Start)(IUnknown* pSinkThisObject, VARIANT_BOOL* succeeded) { AFX_MANAGE_STATE(AfxGetAppModuleState()); // TODO: 在此添加实现代码 if ( DispEventAdvise(pSinkThisObject) == S_OK ) { m_Object = pSinkThisObject; *succeeded = VARIANT_TRUE; } else { *succeeded = VARIANT_FALSE; } return S_OK; } STDMETHOD(Stop)(void) //解除连接 { AFX_MANAGE_STATE(AfxGetAppModuleState()); DispEventUnadvise(m_Object); return S_OK; } 在其他类中的 使用方法: CComPtr m_FileMonitorSink; CComPtr m_FileMonitor; //COM中导出接口 CoInitialize(0); HRESULT lRt = m_FileMonitorSink.CoCreateInstance( __uuidof(FileMonitorSink) ); lRt = m_FileMonitor.CoCreateInstance(__uuidof(Fun1)); //创建COM接口实例 VARIANT_BOOL succeeded; lRt = m_FileMonitorSink->Start(m_FileMonitor, &succeeded); //把 m_FileMonitorSink 连接到COM中的事件容器上 m_FileMonitor->HelloWorld(); //调用COM接口,接口中触发事件s m_FileMonitorSink->stop(); //从COM接口中解除连接 CoUninitialize(); // ################# CFileMonitorSink 类代码 ################# class ATL_NO_VTABLE CFileMonitorSink : public CComObjectRootEx, public CComCoClass, public IDispatchImpl, public IDispEventImpl { public: CFileMonitorSink() { } DECLARE_REGISTRY_RESOURCEID(IDR_FILEMONITORSINK) BEGIN_COM_MAP(CFileMonitorSink) COM_INTERFACE_ENTRY(IFileMonitorSink) COM_INTERFACE_ENTRY(IDispatch) END_COM_MAP() BEGIN_SINK_MAP(CFileMonitorSink) SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 1, OnNotify) SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 2, OnNotify2) END_SINK_MAP() DECLARE_PROTECT_FINAL_CONSTRUCT() HRESULT FinalConstruct() { return S_OK; } void FinalRelease() { } CComPtr m_Object; //COM 事件源对象 public: STDMETHOD(OnNotify)(void); STDMETHOD(Stop)(void); STDMETHOD(Start)(IUnknown* pSinkThisObject, VARIANT_BOOL* succeeded); STDMETHOD(OnNotify2)(CHAR* lszContent); };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值