Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
First of all let me thank you for this wonderful article, which gave insight to the BHO and its functionality.

My aim is to create a BHO which can insert a div to the page and thereby hold an iframe.

Based on the article I created an IEplugin using VS 2010 , C#, IE8. Plugins are properly build and registered and installed but neither the JavaScript nor the div have been inserted to the page.. Herewith I am attaching my code used for the plugin..


IDE - VS 2010
IE version - 8.0


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using SHDocVw;
using mshtml;
using Microsoft.Win32;
 
namespace IEPlugin
{
    [ComVisible(true),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
    Guid("D454ADE9-A3FC-4ae6-B9E9-55D1467C22EE")]
    public interface IObjectWithSite
    {
        [PreserveSig]
        int SetSite([In, MarshalAs(UnmanagedType.IUnknown)]object site);
 
        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);
    }
 

    [ComVisible(true),
    ClassInterface(ClassInterfaceType.None),
    Guid("788EA1C3-124A-4d7a-B693-20B3ED8BFD5D")]
    public class BHO : IObjectWithSite
    {
 
         #region ComREgister/UnRegister Code
 
        public const string BHO_REGISTRY_KEY_NAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
 
        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);
 
            if (registryKey == null)
                registryKey = Registry.LocalMachine.CreateSubKey(BHO_REGISTRY_KEY_NAME);
 
            string guid = type.GUID.ToString("B");
            RegistryKey ourKey = registryKey.OpenSubKey(guid);
 
            if (ourKey == null)
            {
                ourKey = registryKey.CreateSubKey(guid);
            }
 
            ourKey.SetValue("NoExplorer", 1, RegistryValueKind.DWord);
 
            registryKey.Close();
            ourKey.Close();
        }
 
        [ComUnregisterFunction]
        public static void UnregisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);
            string guid = type.GUID.ToString("B");
 
            if (registryKey != null)
                registryKey.DeleteSubKey(guid, false);
        }
 
        #endregion
 

 
        private WebBrowser oWebbrowser;
 
        #region IObjectWithSite Members
 
        public int SetSite(object site)
        {
            if (site != null)
            {
                oWebbrowser = (WebBrowser)site;
                oWebbrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
            }
            else
            {
                oWebbrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
                oWebbrowser = null;
            }
 
            return 0;
        }
 
        public int GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(oWebbrowser);
            int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);
            return hr;
        }
 
        #endregion
 
        #region Eventhandlers
 
        public void OnDocumentComplete(object pDisp, ref object URL)
        {
            //Implement logic to insert Javascript and div elements to the browser

            HTMLDocument oDocument = (HTMLDocument)oWebbrowser.Document;
 
            IHTMLElement oHead = (IHTMLElement)IHTMLElementCollection)oDocument.all.tags("head")).item(null, 0);
 
            IHTMLScriptElement scriptObject = (IHTMLScriptElement)oDocument.createElement("script");
            scriptObject.type = @"text/javascript";
            scriptObject.text = "function hidediv(){document.getElementById('mydiv').style.visibility = 'hidden';}";
            ((HTMLHeadElement)oHead).appendChild((IHTMLDOMNode)scriptObject);
 

            IHTMLElement body = (IHTMLElement)oDocument.body;
            body.insertAdjacentHTML("afterBegin", "<div id=\"mydiv\" style=\"removed:absolute;z-index:2000000;removed50%;removed50%;width:300px;height:300px;margin-removed-150px;margin-removed-150px;background:#000;\"/>Hello World
<a href=\"javascript:hidediv();\">close</a></div>");
        }
 
        #endregion
    }
}


Kindly help me to resolve the issue..
Posted
Updated 20-Jan-13 20:10pm
v4
Comments
jay@begginer 28-Jan-13 8:43am    
I been to this forum with lots of hope with any expert helping me to resolve the issue.. I am in very much need to resolve this issue.. Kindly help me.. Codeproject experts kindly help me to resolve this issue..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900