If the current project does not support ATL, first add to the ATL support 1> Before calling the ATL service (usually in the project stdafx.h) Add below:
#include
2> Add the following code: // The libid_mycomlibrary here is the component library UUID_Module.init (NULL, HINSTANCE, & LIBID_MYCOMLIBRARY) where the event source object is located; _ module.dwthreadID = getCurrentThreadID ();
3> Finally, use the #import directive to import the component library into the project, for example:
#import "e: /test/mycomlibrary.tlb" AMED_GUIDS NO_NAMESPACE
Reference: MSDN Knowledge Base: HOWTO: Add ATL Support to an MFC EXE
Use the iDispeventimpl template provided in the ATL to implement event receiving objects, and add event handling entries with the SINK_ENTRY_EX macro.
I> Create a class to implement the IDispeventimpl interface, hereinafter referred to as an event reception class, that is, a class is derived from the IdispeventImpl template instance. For example, as follows: // Define a unique ID # define IDC_SRCOBJ 1 / / for each event interface to be implemented for event reception classes to define a unique ID to define each event interface to be implemented.
#define idc_srcobj 1
// Declare and implement the event reception class, this class uses the iDispeventimpl template while passing the TypeLibrary / / for the template. It uses sink_entry_ex () macro in the event mapping slot to each event source to specify // component processing functions. class CSinkObj: public IDispEventImpl
STDMETHODIMP MyEventHandler (_BSTR_T STRPAR, long nparam) {MessageBox (Null, "Get MyEvent!", "From csinkobj", MB_OK; Return S_OK;}
STDMETHODIMP MyEvent2Handler (_BSTR_T strparam) {MessageBox (NULL, "Get MyEvent2!", "From csinkobj", mb_ok; returnid;}}; ii> Creating event receiving class // using the event receiving class // created event receiving object csinkobj * psink = new CSinkObj (); // create an event source object of an interface MyClassInterface * pClassInterface = NULL; HRESULT hr = CoCreateInstance (__ uuidof (myComClass), NULL, CLSCTX_INPROC_SERVER, __uuidof (MyClassInterface), (void **) & pClassInterface); if (SUCCEEDED (hr)) {// Connect the event receiving object with the event source component object by an interface of the event source component, PSINK-> DispeventAdVise (PCLASSINTERFAACE); / / Here we call a method in the interface of the event source object Used to inspire an event PCLASSINTERFACE-> Hello (L "TOM");} // Release Event Receive Objects Connection IF (psink) {if (psink-> m_dweventcookie! = 0xfefefefe) psink-> dispeventunadvise (PCLASSINTERFACE); DELETE PSINK; // Release Event Receive Object} // Create Event Receive Object
CsinkObj * psink = new csinkobj ();
// Create an interface of the event source object
MyclassInterface * pclassinterface = null;
HRESULT HR = CoCreateInstance (__ uuidof (mycomclass),
NULL, CLSCTX_INPROC_SERVER,
__UUIDOF (MyClassInterface),
(void **) & pclassInterface;
En (ac))
{
// Connect the event receiving object with the event source component object by an interface of the event source component object.
Psink-> DispeventadVise (PclassInterface);
/ / Here we call a method in the interface of the event source object to inspire an event
PclassInterface-> Hello (L "Tom");
}
// Release Event Receive Objects and Event Source Components Objects
IF (psink)
{
IF (psink-> m_dweventcookie! = 0xfefefefe)
Psink-> Dispeventunadvise; PCLassInterface;
Delete psink; // Release Event Receive Object
}