Question

I created a sample bho using c# and exposed 1 function based from this post

Calling C# BHO methods from Javascript

The bho got registered to IE 9 successfully and i can access the exposed function in javascript on sites hosted in the net.

i created a test page using my local server and my bho function is now undefined.

i tried changing the url from localhost to ip address but still no effect.

below is my code. it would be great if somebody can check if something is wrong with my code;

thanks.

BHO.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.Design;
using SHDocVw;
using mshtml;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using Microsoft.Win32;

namespace MySampleBHO
{
    [
        ComVisible(true),
        Guid("7bb16759-fdfc-4e3f-a081-eb65e4683640"),
        ClassInterface(ClassInterfaceType.None),
        ProgId("SIV"), ComDefaultInterface(typeof(IExtension))
    ]
    public class BHO:IObjectWithSite, IExtension
    {
        WebBrowser webBrowser;
        HTMLDocument document;

        public void OnDocumentComplete(object pDisp, ref object URL) {
            document = (HTMLDocument)webBrowser.Document;

            dynamic window = webBrowser.Document.parentWindow;
            IExpando windowEx = (IExpando)window;
            windowEx.AddProperty("sEnhancer");
            window.sEnhancer = this;
        }

        public String goEnhance(String ImageId, int Width, int Height) {
            var img = document.getElementById(ImageId);
            var src = img.getAttribute("src");
            return src.ToString();
        }

        public int SetSite(object site)
        {
            if (site != null)
            {
                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 static string BHOKEYNAME =
  "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";

        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);

            if (registryKey == null)
                registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);

            string guid = type.GUID.ToString("B");
            RegistryKey ourKey = registryKey.OpenSubKey(guid);

            if (ourKey == null)
                ourKey = registryKey.CreateSubKey(guid);

            ourKey.SetValue("Alright", 1);
            registryKey.Close();
            ourKey.Close();
        }

        [ComUnregisterFunction]
        public static void UnregisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
            string guid = type.GUID.ToString("B");

            if (registryKey != null)
                registryKey.DeleteSubKey(guid, false);
        }
    }
}

IObjectWithSite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MySampleBHO
{
    [
        ComVisible(true),
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
        Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
    ]
    public interface IObjectWithSite
    {
        [PreserveSig]
        int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);

    }
}

IExtension.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MySampleBHO
{
    [
        ComVisible(true),
        InterfaceType(ComInterfaceType.InterfaceIsDual),
        Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
    ]
    public interface IExtension
    {
        String goEnhance(String ImageId, int Width, int Height);
    }
}

Était-ce utile?

La solution

i already found the solution to my problem... just have to change some part in onDocumentComplete function...

Solution

Change below code:

public void OnDocumentComplete(object pDisp, ref object URL) {
  document = (HTMLDocument)webBrowser.Document;

  dynamic window = webBrowser.Document.parentWindow;
  IExpando windowEx = (IExpando)window;
  windowEx.AddProperty("sEnhancer");
  window.sEnhancer = this;
}

to:

public void OnDocumentComplete(object pDisp, ref object URL) {
  document = (HTMLDocument)webBrowser.Document;

  dynamic window = webBrowser.Document.parentWindow;
  IExpando windowEx = (IExpando)window;
  PropertyInfo p = windowEx.AddProperty("sEnhancer");
  p.SetValue(windowEx, this);
}

solution was based from this post BHO exposing javascript method works in IE 9+ but fails in earlier versions

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top