0

I am writing a BHO for IE11. In the BHO I have exposed some methods using IExtension interface to be called by the JavaScript. The Code is below


    IWebBrowser2 _webBrowser2;
    
    int IObjectWithSite.SetSite(object site)
    {
    try
    {
    _site = site;
    
        if (site != null)
        {
            var serviceProv = (IServiceProvider)_site;
            var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp));
            var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2));
    
            IntPtr intPtr;
            serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr);
    
            _webBrowser2 = (IWebBrowser2)Marshal.GetObjectForIUnknown(intPtr);
    
            ((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute += S2_BeforeScriptExecute;
        }
        else
        {
            ((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute -= S2_BeforeScriptExecute;
            _webBrowser2 = null;
        }
    
        return 0;
    }
    
    private void S2_BeforeScriptExecute(object pDispWindow)
    {
        dynamic window = _webBrowser2.Document.parentWindow;
        IExpando windowEx = (IExpando)window;
        windowEx.AddProperty("myExtension");
        window.myExtension = this;
    }

In this everything is working fine except that when user clicks on refresh button in IE, _webBrowser2.Document.parentWindow gives exception

System.NotSupportedException: Exception from HRESULT: 0x800A01B6
   at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
   at CallSite.Target(Closure , CallSite , ComObject )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at CallSite.Target(Closure , CallSite , ComObject )
   at CallSite.Target(Closure , CallSite , Object )
   at BHO.S2_BeforeScriptExecute(Object pDispWindow)

Also I have tried instance of SHDocVw.WebBrowser instead of IWebBrowser2, but still the same issue.

So is there any alternative to this or something else that works fine?

Usama Mohsin
  • 79
  • 1
  • 2
  • 11
  • Yes, you are right, it contains the parentWindow property, I forget to convent the Document to HTMLDocument, so, it doesn't show the parentWindow property on my sample. I have deleted my previous reply. Since, I'm still not find the real reason, can you try to add an if condition to check whether the Document is null before getting the parent window. Like this: `if(webBrowser.Document != null) { dynamic window = (mshtml.HTMLDocument)webbrowser.Document).parentWindow;`. Besides, you could try to use the DocumentComplete event, instead of BeforeScriptExecute event. – Zhi Lv Jun 19 '20 at 12:38
  • I have checked through debugging, Document is not null, parentWindow is. – Usama Mohsin Jun 19 '20 at 13:19
  • Also about using DocumentComplete, IE doesn't fire it on pressing refresh button. – Usama Mohsin Jun 19 '20 at 13:22
  • Please check [this thread](https://stackoverflow.com/questions/15068872/calling-c-sharp-bho-methods-from-javascript-still-not-working), if the site is not null, it is better to add the BeforeScriptExecute event, then, if the site is null, remove the event. code like this:`if(site != null){ webBrowser = (IWebBrowser2)site;((DWebBrowserEvents2_Event)webBrowser).BeforeScriptExecute += S2_BeforeScriptExecute;} else { ((DWebBrowserEvents2_Event)webBrowser).BeforeScriptExecute -= S2_BeforeScriptExecute; webBrowser = null; }` – Zhi Lv Jun 22 '20 at 08:54
  • I am doing the same as you have suggested, also I have followed the link you specified but the issue still exists. – Usama Mohsin Jun 25 '20 at 07:15

0 Answers0