none
Javascript判断 BHO 是否安装? RRS feed

  • 问题

  • 在BHO的代码里
    class ATL_NO_VTABLE CMyBHO :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CMyBHO, &CLSID_MyBHO>,
    public IObjectWithSiteImpl<CMyBHO>,
    public IDispatchImpl<IMyBHO, &IID_IMyBHO, &LIBID_BHOLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
    页面JS判断是否安装过
    try
    {
    //SkypeIEPlugin.SkypeIEPluginBHO.1 //MySampleATL.MyClass
    var myATL = new ActiveXObject("MySampleATL.MyBHO"); // 
    alert(myATL);
    }
    catch(e)
    {
    //return false;  
    alert("false");//我的结果在这里,即没有成功,但是我已经安装过了,并且IE启动项里也可以看见启用了。
    }

    看了以下2个帖子,还是没有明白,我在js里调用都没有成功。

    使用ATL写了一个BHO,请问能在JavaScript里使用BHO中的函数嘛?
    http://topic.csdn.net/t/20061016/15/5085675.html

    JavaScript如何调用BHO的函数。。
    http://topic.csdn.net/u/20090725/09/cf543d3b-8a24-480b-9abf-182d1eb1c1b3.html

    帖子里说得都不是很明白阿。特别是后面这个帖子里 jiangsheng(蒋晟) 说的:
    在DocumentComplete事件的处理用Window的IDispatchEx接口加一个类型为IDispatch的属性 
    脚本里面通过window对象访问。 

    这个能否给段代码看看呢?BHO的DocumentComplete事件和判断的JS代码。

    2010年7月21日 9:43

答案

全部回复

  • http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_20904967.html

    js里面就是和undefined比较



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年7月22日 3:10
    版主
  • http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_20904967.html

    js里面就是和undefined比较



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    //http://msdn.microsoft.com/zh-cn/library/sky96ah7(v=VS.85).aspx


    void NavComplete(){
      HRESULT hr = E_FAIL;
      VARIANT var;
      CComPtr<IDispatch> spDisp;
      hr = m_spWebBrowser2->get_Document(&spDisp);

      if(spDisp == NULL)
      {
       return;
      }
      ATL::CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spDocument2 = spDisp;
      CComPtr<IHTMLWindow2> spWin;
      hr = spDocument2->get_parentWindow(&spWin);
      if(SUCCEEDED(hr)){
       CComQIPtr<IDispatchEx> spWndEx = spWin;
       DISPID dispid;
       DISPPARAMS dispparams, dispparamsNoArgs = {NULL, NULL, 0, 0};
       BSTR bstrName = NULL;
       // Get dispatch pointer for "cat"
       bstrName = SysAllocString("cat");
       hr = spWndEx->GetDispID(bstrName, 0, &dispid);
       SysFreeString(bstrName);
       bstrName = NULL;

       if (SUCCEEDED(hr)){
        hr = spWndEx->InvokeEx(dispid, LOCALE_USER_DEFAULT,
         DISPATCH_PROPERTYGET, &dispparamsNoArgs,
         &var, NULL, NULL);

        if (SUCCEEDED(hr)){

        }
       }
      }
     }

    结合你给的文章与microsoft的文章一起修改的,这个样子是不是表示就可以访问到?但是我页面的方法不会执行阿

    <script language="javascript" type="text/javascript">

    function cat(){
    alert("cat");
    }
    </script>

     

    2010年7月22日 8:55
  • 实现Invoke方法

    STDMETHODIMP Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags,
           DISPPARAMS* pDispParams, VARIANT* pvarResult,
           EXCEPINFO*  pExcepInfo,  UINT* puArgErr)
     {
      if (!pDispParams){return E_INVALIDARG;}

      switch (dispidMember)
      {
       //
       // The parameters for this DISPID are as follows:
       // [0]: Cancel flag - VT_BYREF|VT_BOOL
       // [1]: HTTP headers - VT_BYREF|VT_VARIANT
       // [2]: Address of HTTP POST data - VT_BYREF|VT_VARIANT
       // [3]: Target frame name - VT_BYREF|VT_VARIANT
       // [4]: Option flags - VT_BYREF|VT_VARIANT
       // [5]: URL to navigate to - VT_BYREF|VT_VARIANT
       // [6]: An object that evaluates to the top-level or frame
       // WebBrowser object corresponding to the event.
       //
       case DISPID_BEFORENAVIGATE2:
        
        break;
       case DISPID_NAVIGATECOMPLETE2:
        //for js check
        NavComplete();
        break;
       case DISPID_DOCUMENTCOMPLETE:
        //MessageBox(NULL,_T("lpCaption"),_T("BHO Example - DomComplete event fired!"),MB_OK|MB_ICONINFORMATION);
        Walk();
        break;
       case DISPID_QUIT:
       
        RegisterEventHandler(FALSE);
        break;
       default :
        break;
      }
      return S_OK;
     }

    2010年7月22日 8:57
  • 还是没有成功
    2010年7月23日 3:35
  • http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_20904967.html

    js里面就是和undefined比较



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    底下的解决方法看不到。。。。。
    2010年7月23日 3:49
  • 不好意思,看错了,成功实现了。 window.cat就有了。
    2010年7月23日 3:54