Re: Default Implementation for IUnknown
A callback object usable with attachEvent is completely different from
an event sink attached with the connection point. The latter does need
to implement DIID_HTMLElementEvents2 and handle events with various
DISPIDs like DISPID_HTMLELEMENTEVENTS2_ONCLICK. The former does not and
does not. Its Invoke implementation is only ever called with a DISPID of
DISPID_VALUE.
And how did you find that out? From the MSDN reference I could not
figure that out. Actually, I was thinkin that pub_onclick/attachEvent
connect to an connection point in the background anyway. For example,
I tried to fetch a connection point for the HTMLElementEvents
interface from a div element and it worked, so I assumed that it is an
ordinary connection point.
Since the callback does not implement any particular named
dispinterface
According to the reference HTMLElementEvents2 is a dispinterface
(http://msdn2.microsoft.com/en-us/library/aa769636.aspx). So I assumed
that I have to implement this in ordert to catch its events.
I tried your test code again:
class ATL_NO_VTABLE DOMEventHandler :
public IDispEventSimpleImpl<1, DOMEventHandler, &IID_IDispatch> {
//public IDispEventImpl<1, DOMEventHandler, &DIID_HTMLElementEvents2,
&LIBID_MSHTML, 1, 1> {
typedef IDispEventSimpleImpl<1, DOMEventHandler, &IID_IDispatch>
BaseClass;
public:
DOMEventHandler(void) {}
virtual ~DOMEventHandler(void) {}
VARIANT_BOOL STDMETHODCALLTYPE OnEvent();
BEGIN_SINK_MAP(DOMEventHandler)
//SINK_ENTRY_EX(1, DIID_HTMLElementEvents2,
DISPID_HTMLELEMENTEVENTS2_ONCLICK, OnClick)
SINK_ENTRY_INFO(1, IID_IDispatch, DISPID_VALUE, OnEvent, &EventInfo)
END_SINK_MAP()
IDispatch* GetIDispatch() {
return
reinterpret_cast<IDispatch*>(static_cast<BaseClass*>(this));
}
private:
static _ATL_FUNC_INFO EventInfo;
};
and registering with
DOMEventHandler* pDEH = new DOMEventHandler();
IDispatch* pDEHDisp = pDEH->GetIDispatch();
CComQIPtr<IHTMLElement> spFirstChildElement = /* initialized somehow
*/;
hr = spFirstChildElement->put_onclick( CComVariant(pDEHDisp) );
Again, I get the read access violation again. In fact, the problem
arises from an pDEHDisp->AddRef call in the CComVariant constructor.
The the debugger's locals view I see the following:
http://img181.imageshack.us/img181/8455/localsvb7.jpg This makes me
think that something is wrong with the vtable and thus the read access
violation occurs. However, I have no idea why this is wrong and how to
fix it... do you?