1

So I have wrote a BHO that exposes a function that I call from Javascript. My target system is Windows 8 IE 10 but the behavior that I get is same on Windows 7,8 32 and 64 bit, IE 10, IE 11 - it doesnt matter, I concluded it`s not system related.

So BHO works fine if you open a page that has JavaScript that calls function from a BHO - it gets defined and everything is good. I have a simple local page to test it that calls BHO.method(), same page hosted online also works great.

But in the following cases it doesnt work - i get "error: *property_added* is undefined". 1) if you open the same test page with JS code in a new additional tab. (If in just a single tab - it works as I`ve described above). 2) if you open any other page in a first tab and then get redirected using link to (or just paste in the address bar the URL of the) same old test page - "error: *property_added* is undefined".

Now the interesting part - I add a property (let`s call it) *property_added* in DocumentComplete event. I put alerts there I can see that DocumentComplete and AddProperty fires and runs at all the right times, for every page, but in cases that I describe I get this error on 'dynamic window = browser.Document.parentWindow;' - Exception from HRESULT: 0x800A01B6. Please help?

Below is the code that I use in OnDocumentComplete - it`s pretty standard though.

void OnDocumentComplete(object pDisp, ref object URL)
    {
        try
        {
            SHDocVw.IWebBrowser2 explorer = pDisp as SHDocVw.IWebBrowser2;

            if (explorer != null)
            {                                        
                dynamic window = explorer.Document.parentWindow;

                IExpando windowEx = (IExpando)window;

                PropertyInfo property = windowEx.GetProperty("BHO",BindingFlags.IgnoreCase); //BindingFlags.Default);
                if (property == null)
                {                        
                    property = windowEx.AddProperty("BHO");                        
                }
                property.SetValue(windowEx, this, null);                    
                window.execScript("alert();");                  
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);                
        }
    }


int IObjectWithSite.SetSite(object site)
    {
        this.site = site;
        if (site != null)
        {
            browser = (InternetExplorer)site; 

            ((DWebBrowserEvents2_Event)browser).DocumentComplete +=
                new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
        }
        return 0;
    }

UPDATE: When I add the following string

window.execScript("alert('BHO is '+(typeof(BHO) == 'undefined' ? 'undefined' : 'defined'));");

in the OnDocumentComplete right after property.SetValue(windowEx, this, null);- redirected page case starts working fine all of a sudden. If I remove this string - it doesn`t work. How in the world this makes a difference??

  • You should get the `Document.parentWindow` via the `pDisp`. – manuell Feb 13 '14 at 17:07
  • dynamic window = browser.Document.parentWindow - I changed it to explorer.Document.parentWindow - which means I am taking it from pDisp, right? Same thing, same exception at the same place. – user1234515 Feb 13 '14 at 21:48
  • I make BHOs using C++ and always use the COM IWebBrowser2 interface in OnDocumentComplete (from the pDisp). Don't know what is that `InternetExplorer` class, and if it's the right one to get at the Window. – manuell Feb 14 '14 at 09:44
  • I don't understand your edit. You said that you where getting an Exception on `dynamic window = [...] ` and you are now saying that adding new code AFTER that line makes the exception disapear? – manuell Feb 14 '14 at 09:46
  • See the `SetSite` method here: http://stackoverflow.com/questions/5643819/developing-internet-explorer-extensions The browser member is an `IWebBrowser2`. – manuell Feb 14 '14 at 09:54
  • This is correct. Just adding alert AFTER makes it work. I am surprised as much as you are, believe me. – user1234515 Feb 14 '14 at 14:21
  • You may try to refactor your code, using the IWebBrowser2 interface obtained from the pDisp, (or stored while in SetSite if you always want to go with the main frame). See the SO link I gave you. – manuell Feb 14 '14 at 14:48
  • Yes, I am getting the exception on 'dynamic window = ...' and when I add javascript alert to the 'window' - it starts working fine all of a sudden. I am as just surprised as you are. I will update the code with that last example. Also I tried the SetSite from that stackoverflow question in the first place - same behavior. And I did change InternetExplorer control to IWebBrowser2 control - no change, same behavior. – user1234515 Feb 14 '14 at 18:22

0 Answers0