Search code examples
c#internet-explorerbho

BHO only working in first IE window or tab open


I've been reading and following up on how to write a BHO in IE using C# and I can register it just fine and run it but it only works properly when in the first window\tab I've got open.

I know aspects of it are triggering in every new window but the changes don't "stick" or apply if they're affecting the DOM. So, for example, I load a page that displays some text in the top of the page, it will always be there in the first tab but all the others it may be there are first then disappear or not show up at all.

I'm using c# 4 on Win7x64 using IE11. Protected mode doesn't appear to affect this one way or the other.

My code is just a mix of what's up here tutorial wise, so nothing fancy.

namespace IEExtention

{ [ ComVisible(true), Guid("e8483cfd-d208-45f7-837c-3cdca573d84a"), ClassInterface(ClassInterfaceType.None) ]

public class BHO : IObjectWithSite
{
    private WebBrowser webBrowser;
    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private object mySite;

    public int SetSite(object site)
    {
        if (site != null)
        {
            mySite = site;
            webBrowser = (WebBrowser)site;
            webBrowser.DocumentComplete +=
              new DWebBrowserEvents2_DocumentCompleteEventHandler(
              this.OnDocumentComplete);
        }
        else
        {
            webBrowser.DocumentComplete -=
              new DWebBrowserEvents2_DocumentCompleteEventHandler(
              this.OnDocumentComplete);
            webBrowser = null;
        }

        return 0;


    }

    public int GetSite(ref Guid guid, out IntPtr ppvSite)
    {
        IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
        int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
        Marshal.Release(punk);
        return hr;
    }

public void OnDocumentComplete(object pDisp, ref object URL)
    {
        log.Debug("test");
        if (pDisp != mySite)
        {
            return;
        }
HTMLDocument document = (HTMLDocument)this.webBrowser.Document;
            document.title = "Hello, StackOverflow!";
            try
            {
                IHTMLDOMNode greetings = document.createTextNode("Hi there!");

                IHTMLDOMNode body = document.body as IHTMLDOMNode;
                body.insertBefore(greetings, body.firstChild);
            }
            catch (Exception e)
            {
                //whoo!!
            }
}

It's had me stumped for a few days as even something as changing the document.title doesn't always stay.


Solution

  • I was able to work around this issue by threading my BHO and sleeping it for about half a second. Interestingly enough I needed to up the sleep to about 1.5 seconds to deal with outside links (say something coming from outlook) to load up and get everything to display.

    I'm not sure if this is the best way to do it but it solved my problem with it only working in the first tab.