从 Javascript 调用 BHO 方法? [英] Calling BHO method from Javascript?

查看:23
本文介绍了从 Javascript 调用 BHO 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 javascript 调用我的 BHO 方法.问题与以下帖子中所述相同:

I am trying to call my BHO method from the javascript. The problem is same as stated in the the following posts:

  1. 从 Javascript 函数调用 BHO
  2. http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/91d4076e-4795-4d9e-9b07-5b9c9eca62fb/
  3. 从 JavaScript 调用 C++ 函数在 Web 浏览器控件中运行的脚本

第三个链接 是另一个 SO 帖子谈论它,但我不明白需要和代码.此外,共享的工作示例在使用 ie 8 的 Windows 7 和使用 ie 7 的 windows vista 上不断崩溃.

Third link is another SO post talking about it, but I did not understand the need and code. Also the shared working sample keeps crashing on windows 7 with ie 8 and windows vista with ie 7.

如果它有帮助,我的 BHO 是使用 ATL 用 C++ 编写的.

If it helps my BHO is written in C++ using ATL.

我尝试过的:

我编写了一个非常基本的 BHO 并尝试了上述方法 此处 作者 Igor Tandetnik.没有生成异常,但是当我在 IE 中打开以下 html 文件时,它显示 object undefined.

I have written a very basic BHO and tried the approach as mentioned here by Igor Tandetnik. There is no exception generated but when I open the following html file in IE then it says object undefined.

<html>
    <head>
        <script language='javascript'>
            function call_external(){
                try{
                alert(window.external.TestScript);
                //JQueryTest.HelloJquery('a');
                }catch(err){
                    alert(err.description );
                }
            }
        </script>
    </head>
    <body id='bodyid' onload="call_external();">
        <center><div><span>Hello jQuery!!</span></div></center>
    </boay>
</html>

问题:

  1. 请说明是否可以从 javascript 公开和调用 BHO 方法,或者我是否必须使用 activex 公开它(如 jeffdav[2] )?如果是,那么该怎么做.
  2. 基本上我想扩展 window.external 但上面链接中显示的方式 [2] 使用 var x = new ActiveXObject("MySampleATL.MyClass");;两个调用约定是相同的还是不同的?
  1. Please clarify whether is it possible to expose and call BHO method from javascript or do i have to expose it using an activex (as answered by jeffdav in [2] )? If yes then how to do it.
  2. Basically i want to extend the window.external but the way shown in the above link [2] uses var x = new ActiveXObject("MySampleATL.MyClass");; Is both the calling conventions are same or different?

注意:

  1. SO 上有一篇相关的帖子,提示可以通过在 BHO IDL 中插入此 [id(1), helpstring("method DoSomething")] HRESULT DoSomething();文件.我不确定它是如何完成的,也无法通过 google 找到任何支持资源.
  2. 我知道这篇文章 calling-into-your-bho-from-a-client-script,但没有尝试过,因为它正在使用 ActiveX 解决问题.
  3. 我避免使用 ActiveX 的主要原因是安全限制.
  1. There is a related post on SO which gives hint that it is possible through inserting this [id(1), helpstring("method DoSomething")] HRESULT DoSomething(); in the BHO IDL file. I am not sure how it was done and couldn't find any supporting resource through google.
  2. I am aware of this post calling-into-your-bho-from-a-client-script, but haven't tried it as it is solving the problem using ActiveX.
  3. My reason to avoid ActiveX is primarily due to the security restrictions.

编辑 1<小时>似乎有一种方法可以扩展 window.external.检查这个.特别是标题为 IDocHostUIHandler::GetExternal:扩展 DOM 的部分.现在假设我们的 IDispatch 接口位于实现 IDocHostUIHandler 的同一对象上.然后我们可以做这样的事情:

Edit 1


It seems there is a way to extend the window.external. Check this. Specially the section titled IDocHostUIHandler::GetExternal: Extending the DOM.Now assuming our IDispatch interface is on the same object that implements IDocHostUIHandler. Then we can do something like this:

HRESULT CBrowserHost::GetExternal(IDispatch **ppDispatch) 
{
    *ppDispatch = this;
    return S_OK;
}

这种方法的问题是它不会附加到现有的 windows 方法,而是替换它们.如果我错了,请告诉我.

The problem with this approach is that it won't append to the existing windows methods, but rather replace them. Please tell if I am wrong.

编辑 2<小时>BHO 类:

class ATL_NO_VTABLE CTestScript :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CTestScript, &CLSID_TestScript>,
    public IObjectWithSiteImpl<CTestScript>,
    public IDispatchImpl<ITestScript, &IID_ITestScript, &LIBID_TestBHOLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
    public IDispEventImpl<1, CTestScript, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>
{
public:
    CTestScript()
    {
    }

DECLARE_REGISTRY_RESOURCEID(IDR_TESTSCRIPT)

DECLARE_NOT_AGGREGATABLE(CTestScript)

BEGIN_COM_MAP(CTestScript)
    COM_INTERFACE_ENTRY(ITestScript)
    COM_INTERFACE_ENTRY(IDispatch)
    COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()



    DECLARE_PROTECT_FINAL_CONSTRUCT()

    HRESULT FinalConstruct()
    {
        return S_OK;
    }

    void FinalRelease()
    {
    }

public:
    BEGIN_SINK_MAP(CTestScript)
        SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
        //SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_NAVIGATECOMPLETE2, OnNavigationComplete)
    END_SINK_MAP()

    void STDMETHODCALLTYPE OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL);
    //void STDMETHODCALLTYPE OnNavigationComplete(IDispatch *pDisp, VARIANT *pvarURL);

    STDMETHOD(SetSite)(IUnknown *pUnkSite);

    HRESULT STDMETHODCALLTYPE DoSomething(){
        ::MessageBox(NULL, L"Hello", L"World", MB_OK);
        return S_OK;
    }
public:

//private:
    // InstallBHOMethod();

private:
    CComPtr<IWebBrowser2>  m_spWebBrowser;
    BOOL m_fAdvised;
};

//TestScript.cpp : CTestScript的实现

#include "stdafx.h"
#include "TestScript.h"


// CTestScript

STDMETHODIMP CTestScript::SetSite(IUnknown* pUnkSite)
{
    if (pUnkSite != NULL)
    {
        HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
        if (SUCCEEDED(hr))
        {
            hr = DispEventAdvise(m_spWebBrowser);
            if (SUCCEEDED(hr))
            {
                m_fAdvised = TRUE;              
            }
        }
    }else
    {
        if (m_fAdvised)
        {
            DispEventUnadvise(m_spWebBrowser);
            m_fAdvised = FALSE;
        }
        m_spWebBrowser.Release();
    }
    return IObjectWithSiteImpl<CTestScript>::SetSite(pUnkSite);
}

void STDMETHODCALLTYPE CTestScript::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{
        CComPtr<IDispatch> dispDoc;
        CComPtr<IHTMLDocument2> ifDoc;
        CComPtr<IHTMLWindow2> ifWnd;
        CComPtr<IDispatchEx> dispxWnd;

        HRESULT hr = m_spWebBrowser->get_Document( &dispDoc );
        hr = dispDoc.QueryInterface( &ifDoc );      
        hr = ifDoc->get_parentWindow( &ifWnd );
        hr = ifWnd.QueryInterface( &dispxWnd );

        // now ... be careful. Do exactly as described here. Very easy to make mistakes
        CComBSTR propName( L"myBho" );
        DISPID dispid;
        hr = dispxWnd->GetDispID( propName, fdexNameEnsure, &dispid );

        CComVariant varMyBho( (IDispatch*)this );
        DISPPARAMS params;
        params.cArgs = 1;
        params.cNamedArgs = 0;
        params.rgvarg = &varMyBho;            
        params.rgdispidNamedArgs = NULL;
        hr = dispxWnd->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT,
            &params, NULL, NULL, NULL );

}

Javascript:

<script language='javascript'>
            function call_external(){
                try{
                alert(window.ITestScript);
                }catch(err){
                    alert(err.description );
                }
            }
        </script>

编辑 3<小时>在这上面花了三天时间后,我想我应该走 ActiveX 路径.编写一个基本的 activex 只是一种简单的方法,编写了一个并在所有主要的 IE 版本上进行了测试.我将这个问题保持开放,请参阅 Uri 回答中的评论(非常感谢他).我已经尝试了他的大部分建议(除了 4 和 5).我还建议您查看 MSDN IDispatcEx 示例.如果您找到解决方案,请发布,如果我找到解决方案,那么我一定会在这里更新.

Edit 3


After spending three days on this, I think I should take the ActiveX path. Writing a basic activex is just way to easy, written one and tested on all major IE releases. I am leaving this question as open, please see comments in Uri's answer (many thanks to him). I have tried most of his suggestions (except for 4 and 5). I will also suggest you to see the MSDN IDispatcEx sample. If you find a solution then please post, if I find a solution then I will definitely update here.

编辑 4<小时>查看我在 URI 帖子中的最后一条评论.问题已解决.

推荐答案

Igor Tandetnik 的方法是正确的方法.这篇文章的问题在于示例代码(至少在我发现的几页上)是不完整的.在我让它工作之前,我经历了很多尝试和错误.这是我的一大段代码,可以解决问题:

Igor Tandetnik's method is the correct approach. The problem with the post is that the sample code (at least on the few pages I spotted it) is that it wasn't complete. I had many trials and errors until I got it working. Here is a good chunk of my code that does the trick:

假设您有一个 CMyBho 类,并且您想为 Java 脚本公开 IMyBho 自动化对象

Say you have a class CMyBho, and you want to expose IMyBho automation object for Java scripts

类定义:
您从标准的 CComObjectRootEx 和 CComCoClass 派生,使其成为共同创造".你有 IObjectWithSiteImpl(重用由这个基类实现的 m_spUnkSite).IDispatchImpl 实现您的自动化对象,而 IDispatchEventImpl 是从浏览器获取通知的接收器:

Class definition:
You derive from the standard CComObjectRootEx, and CComCoClass to make it 'co creatble'. You have IObjectWithSiteImpl (reuse the m_spUnkSite implemented by this base class). IDispatchImpl implements your automation object, and IDispatchEventImpl is the sink to get notifications from the browser:

class ATL_NO_VTABLE CMyBho
    : public CComObjectRootEx<CComSingleThreadModel>
    , public CComCoClass<CMyBho, &CLSID_MyBho>
    , public IObjectWithSiteImpl<CMyBho>
    , public IDispatchImpl<IMyBho, &IID_IMyBho, &LIBID_MyBhoLib, 1, 0>
    , IDispatchEventImpl<1, CMyBho, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>
{
    ...

public:
    BEGIN_COM_MAP(CMyBho)
        COM_INTERFACE_ENTRY(IMyBho)
        COM_INTERFACE_ENTRY(IDispatch)
        COM_INTERFACE_ENTRY(IObjectWithSite)
    END_COM_MAP()

    ...

    BEGIN_SINK_MAP(CMyBho)
        SINK_ENTRY_EX( 1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocComplete )
    END_SINK_MAP()

    ...

private:
    CComPtr<IWebBrowser2> m_ifbrz;          // pointer to the hosting browser

}

接下来是 SetSite 方法,您可以在其中注册以获取通知.不要忘记调用基类.

Next, the SetSite method, where you register to get the notification. Don't forget to call the base class.

STDMETHODIMP CMyBho::SetSite( IUnknown* unkSite )
{
    ...
    hr = IObjectWithSiteImpl::SetSite( unkSite );
    if( unkSite ) {
        ...
        // advise to browser event.
        CComPtr<IServiceProvider> ifsp;
        hr = m_spUnkSite.QueryInterface( &ifsp );
        hr = ifsp->QueryService( SID_SwebBrowserApp, IID_IWebBrowser2, &m_ifbrz );
        hr = DispEventAdvise( m_ifbrz );
    }
    else {
        // release various resources (m_ifbrz will be released automatically by its dtor)
        ...
    }
...
}

当文档加载完成时,将调用此函数:

When document load is complete, this function will be called:

void STDMETHODCALLTYPE CMyBho::onDocComplete( IDispatch* dispBrz, VARIANT* pvarUrl )
{
    CComPtr<IDispatch> dispDoc;
    CComPtr<IHTMLDocument2> ifDoc;
    CComPtr<IHTMLWindow2> ifWnd;
    CComPtr<IDispatchEx> dispxWnd;

    hr = m_ifbrz->get_Document( &dispDoc );
    hr = dispDoc.QueryInterface( &ifDoc );      
    hr = ifDoc->get_parentWindow( &ifWnd );
    hr = ifWnd.QueryInterface( &dispxWnd );

    // now ... be careful. Do exactly as described here. Very easy to make mistakes
    CComBSTR propName( L"myBho" );
    DISPID dispid;
    hr = dispxWnd->GetDispID( propName, fdexNameEnsure, &dispid );

    CComVariant varMyBho( (IDispatch*)this );
    DISPPARAMS params;
    params.cArgs = 1;
    params.cNamedArgs = 0;
    params.rgvarg = &varMyBho;            
    params.rgdispidNamedArgs = NULL;
    hr = dispxWnd->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUTREF,
                           &params, NULL, NULL, NULL );
}

至于您的其他问题:

  • 显然,我的回答暗示您可以使自动化对象可用于从 BHO 编写脚本.也有可能您的对象将使用新的 ActiveXObject 进行实例化.在这种情况下,请不要忘记告诉 IE 您的对象可以安全用于脚本编写(旁注:确保您的 BHO 可以安全用于脚本编写.确保恶意网站无法利用您的 BHO).

  • Evidently, my answer implies that you can make an automation object available for scripting from your BHO. It is also possible your object will be instantiated with the new ActiveXObject. In that case don't forget to tell IE your object is safe for scripting (side note: make your BHO safe for scripting. make sure malicious web site won't be able to exploit your BHO).

我认为 window.myBho 比 window.external.myBho 更好.从语义上讲,外部"是当 mshtml浏览器控件托管在另一个应用程序中.

I think that window.myBho is a better place than window.external.myBho. Semantically, 'external' is when the mshtml browser control is hosted within another application.

希望这有帮助.

这篇关于从 Javascript 调用 BHO 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆