(IE BHO) Get IWebBrowser2 from BeforeNavigate2 event

I'm working on an IE BHO, in the BeforeNavigate2 event, how can I get the pointer to IWebBrowser2? here's my code:
STDMETHODIMP CEventSink::Invoke(DISPID dispIdMember, .....) {
    IWebBrowser2* pSite = 0;
    HRESULT hr;

if(dispIdMember == DISPID_BEFORENAVIGATE2) {

    hr = ((IDispatch*)pDispParams->rgvarg[0].pdispVal)->QueryInterface(IID_IWebBrowser2, (void**)&pSite);// This line Crashes 

the MSDN http://msdn.microsoft.com/en-us/library/aa768280%28VS.85%29.aspx
says the first parameter is
A pointer to the IDispatch interface for the WebBrowser object that represents the window or frame. This interface can be queried for the IWebBrowser2 interface.

What's wrong with my code ?

Thanks in advance.
You shouldn't need the IDispatch* cast as that's the type of pdispVal (casts should not be used unnecessarily.)

Is the vt (VARTYPE) member of rgvarg[0] set to VT_DISPATCH as you expect? (pdispVal is only valid if this is the case.)

What hr value is QueryInterface returning? Or is your code blowing up??

Last edited on
Thanks, it turns out that the parameters are in the reverse order, so rgvarg[6] is the correct arg.
Topic archived. No new replies allowed.