Getting IWebBrowser2 pointer from event DISPID_TITLECHANGE
Im working on a Browser Helper Obje开发者_StackOverflow中文版ct, 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.
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.
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);
}
精彩评论