Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

internet explorer - Call BHO from Javascript function

Greetings,

I am new to MSDN/Windows programming/MFC/IE extension development environment. I am looking to port my Firefox and Chrome addon into an IE addon. I have been successful to a good extent in creating a BHO and have been able to inject my code into every IE window as noted here - http://msdn.microsoft.com/en-us/library/bb250489%28v=vs.85%29.aspx

I am only stuck with the issue of being able to call the BHO function from javascript. Just need help in being able to call

window.HelloWorld.StoreInfo() from javascript. Please see my code snippet below. My javascript recognizes window.HelloWorld, but not the function inside it.

I suspect the IDispatchEx has something to do with this.

    STDMETHODIMP CHelloWorldBHO::SetSite(IUnknown* pUnkSite)
    {
        if (pUnkSite != NULL)
        {
            // Cache the pointer to IWebBrowser2.
            HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
            if (SUCCEEDED(hr))
            {
                // Register to sink events from DWebBrowserEvents2.
                hr = DispEventAdvise(m_spWebBrowser);
                if (SUCCEEDED(hr))
                {
                    m_fAdvised = TRUE;
                }
            }
        }
        else
        {
            // Unregister event sink.
            if (m_fAdvised)
            {
                DispEventUnadvise(m_spWebBrowser);
                m_fAdvised = FALSE;
            }

            // Release cached pointers and other resources here.
            m_spWebBrowser.Release();
        }

        // Call base class implementation.
        return IObjectWithSiteImpl<CHelloWorldBHO>::SetSite(pUnkSite);
    }

    void STDMETHODCALLTYPE CHelloWorldBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
    {
        HRESULT hr = S_OK;

        // Query for the IWebBrowser2 interface.
        CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;

        // Is this event associated with the top-level browser?
        if (spTempWebBrowser && m_spWebBrowser &&
            m_spWebBrowser.IsEqualObject(spTempWebBrowser))
        {
            // Get the current document object from browser...
            CComPtr<IDispatch> spDispDoc;
            hr = m_spWebBrowser->get_Document(&spDispDoc);
            if (SUCCEEDED(hr))
            {
                // ...and query for an HTML document.
                CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
                if (spHTMLDoc != NULL)
                {
                    CComQIPtr<IHTMLWindow2> pWindow;
                    spHTMLDoc->get_parentWindow(&pWindow);

                    CComQIPtr<IDispatch> pScriptDisp(pWindow);
                    IDispatchEx *pScriptDispEx;
                    pWindow->QueryInterface(&pScriptDispEx);


                    DISPID did;
                    hr = pScriptDispEx->GetDispID(CComBSTR("HelloWorld"), fdexNameEnsure, &did);
                    CComVariant dest;
                    DISPPARAMS dp = { 0, 0, 0, 0 };
                    hr = pScriptDispEx->Invoke( did, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dp, &dest, NULL, NULL );

                }
            }
        }
    }

    BOOL CHelloWorldBHO::StoreInfo()
    {
        // Store params passed from javascript

    return true;
}

I do not wish to use ActiveX primarily due to the security restrictions.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to assign an automation object who expose a StoreInfo parameterless method to the window.HelloWorld property (InvokeEx with DISPATCH_PROPERTYPUT). Right now you are not assigning anything to the property.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...