Call BHO from Javascript function

江枫思渺然 提交于 2019-11-27 03:42:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5474808/call-bho-from-javascript-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!