Trying to handle events from HTMLElementEvents...
 
Hi all,
I'm having problem in handling the ONCLICK event from HTMLElementEvents
I've connect the interface to my Sink object- code follow - but when 
accessing to ElementObject I got an access violation...
To catch the event I've derived a class from CCmdTarget:
class CEventSink : public CCmdTarget
{
public:
    void OnClick( IHTMLEventObj *pEvtObj );
    DECLARE_DISPATCH_MAP()
};
in cpp:
// For HTML Events
BEGIN_DISPATCH_MAP(CEventSink, CCmdTarget)
    DISP_FUNCTION_ID(CEventSink, "OnClick", DISPID_HTMLELEMENTEVENTS2_ONCLICK, 
OnClick, VT_EMPTY, VTS_VARIANT )
END_DISPATCH_MAP()
// END HTML Events
void CEventSink::OnClick(IHTMLEventObj *pEvtObj)
{
    AfxMessageBox( _T("ONCLICK sink") );
}
each time I access to pEvtObj I got an access violation...
To connect tht interface I've used DIID_HTMLButtonElementEvents because 
DIID_HTMLElementEvents is not supported by the HTMLElement... I do not know 
why!!
The element is a button inside a html page...
here is the code I've used to connect the event interface:
HRESULT					hr;
    LPDISPATCH			pBrowserDispatch				= NULL;
    IUnknown*				pUnkBrowser					= NULL;
    IHTMLDocument2*	pDocument2					= NULL;
    IDispatch*				pDocumentDispatch			= NULL;
    pUnkBrowser = m_Browser.GetControlUnknown();
    if( !pUnkBrowser )
        return;
    pUnkBrowser->QueryInterface( IID_IDispatch, (void**)&pBrowserDispatch );
    if( !pBrowserDispatch )
        return;
    if( pDisp == pBrowserDispatch )
    {
        pDocumentDispatch = m_Browser.get_Document();
        if( pDocumentDispatch )
        {
            pDocumentDispatch->QueryInterface( IID_IHTMLDocument2, 
(void**)&pDocument2 );
            BOOL a = AfxConnectionAdvise( pDocument2, DIID_HTMLDocumentEvents2, 
m_pEventSink->GetIDispatch(FALSE), FALSE, &m_dwEventSink );
            IHTMLElementCollection* pElemCollection = NULL;
            hr = pDocument2->get_all( &pElemCollection );
            if( SUCCEEDED(hr) )
            {
                // Obtained element collection.
                IDispatch*			pElementDispatch		= NULL;
                IHTMLElement*	pElement				= NULL;
                pElemCollection->item( COleVariant( _T("ale")), COleVariant((long)0), 
&pElementDispatch );
                if( pElementDispatch )
                {
                    hr = pElementDispatch->QueryInterface(IID_IHTMLElement, 
(void**)&pElement);
                    if( pElement )
                    {
                        //ConnectEvents( pElement );
                        AfxConnectionAdvise( pElement, DIID_HTMLButtonElementEvents, 
m_pEventSink->GetIDispatch(FALSE), FALSE, &m_dwEventSink );
                        pElement->Release();
                    }
                    pElementDispatch->Release();
                }
                pElemCollection->Release();
            }
            pDocument2->Release();
        }
    }
    pBrowserDispatch->Release();
Thanks
Ale