winapicomatlidispatchatlcom

How to create anonymous IDispatch functions with ATL?


Is there an easy way to create a class that is derived from IDispatch and that can be passed to API like this (this is C++ ):

pDOM2->attachEvent(CComBSTR("event"), new DispatchFunction);

I know how to create a C++ object that handle an invoke with a dispid of value 0 but it's always a bit painful to implement all IDispatch by hand. I was wondering if ATL has some standard way of doing it as it is fairly common.


Solution

  • By default ATL creates "auto-dual" COM classes. They support both early binding and IDispatch. You'll see IDispatchImpl<> in their inheritance list. You declare the dispid in the IDL. No extra work is required.