开发者

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 bei开发者_JAVA百科ng 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜