0

Im working on a Browser Helper Object, and I am trying to access the IWebBrowser2 that fires an event. With NavigateComplete2 and other events I can easly do it because I get the pointer on the parameters of Invoke.

But I was reading this on msdn and it says the only parameter for TitleChange event is the title, so how do I get the pointer to the webbrowser interface from the event TitleChange?

Here's how I am getting it with other events:

HRESULT STDMETHODCALLTYPE CSiteEvents::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, 
                                         DISPPARAMS __RPC_FAR *Params, VARIANT __RPC_FAR *pVarResult, 
                                         EXCEPINFO __RPC_FAR *pExcepInfo, UINT __RPC_FAR *puArgErr )
{
    switch ( dispIdMember )
    {
        case DISPID_DOCUMENTCOMPLETE:
        {
            IWebBrowser2 *pBrowser = GetBrowser(Params->rgvarg[1]);
            // stuff
            pBrowser->Release();
        }
        break;
    }
}

IWebBrowser2* GetBrowser(const VARIANT &_Argument)
{
    IWebBrowser2 *pBrowser = NULL;

    if (_Argument.vt == VT_DISPATCH)
    {
        HRESULT hr;
        IDispatch *pDisp = _Argument.pdispVal;

        if (pDisp)
        {
            hr = pDisp->QueryInterface( IID_IWebBrowser2, reinterpret_cast<void **>(&pBrowser) );
            if ( FAILED(hr) )
                pBrowser = NULL;
        }
    }

    return pBrowser;
}

I am using Visual Studio 2010.

gtilx
  • 2,055
  • 4
  • 17
  • 21

2 Answers2

0

Isn't the IDispatch context here implicit? With the other events, you have to distinguish whereabouts in the control the event happened, while for TitleChange it's at the top level - this means this is an IDispatch* that can be queried to get the interface you need.

DWebBrowserEvents2 inherits from IDispatch but also encapsulate another IDispatch for each component of the window.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • Do you mean I should be able to use: IWebBrowser2 *pBrowser = NULL; this->QueryInterface( IID_IWebBrowser2, reinterpret_cast(&pBrowser) );? I tried that and pBrowser is NULL. – gtilx Nov 07 '10 at 16:32
  • That's what I thought should work - sorry that did not do it... What happens when you try this using the UUID for `IDispatch`? – Steve Townsend Nov 07 '10 at 18:50
  • How can I use the UUID? do you have some example? – gtilx Nov 08 '10 at 05:16
0

Title can be changed only in main window, so you can use IWebBrowser2, retrieved from IUnknown passed to your SetSite implementation.

STDMETHODIMP CMyBHO::SetSite(IUnknown *punkSite)
{
    if(punkSite != NULL)
    {
        // CComPtr<IWebBrowser2> m_pWebBrowser is member of CMyBHO class
        CComQIPtr<IServiceProvider> pServiceProvider = punkSite;
        if(pServiceProvider != NULL)
            pServiceProvider->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (void**)&m_pWebBrowser);   
    }
    else
    {
        if(m_pWebBrowser != NULL)
        {
            m_pWebBrowser = NULL;
        }
    }
    return IObjectWithSiteImpl<CMyBHO>::SetSite(punkSite);
}
KAdot
  • 1,997
  • 13
  • 21