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.