简体   繁体   中英

Internet Explorer 11 Extension (BHO) with C#

I am trying to write an extension for Internet Explorer 11 as a Browser Helper Object in Visual C#. I'm using Visual Studio 2013 on Windows 7. With zilch experience here I've run in to a brick wall trying to get off the ground. I'm not exactly sure how to implement the interface IObjectWithSite for the BHO. I've tried something basic to get me started, but it doesn't work...any ideas?

using System;
using System.Runtime.InteropServices;
using SHDocVw;
using mshtml;
using System.Windows.Forms;

namespace IeAddOnDemo
{
    [ComVisible(true),
    Guid("9AB12757-BDAF-4F9A-8DE8-413C3615590C"),
    ClassInterface(ClassInterfaceType.None)]
    public class BHO : IObjectWithSite
    {
        public void SetSite(object site)
        {
            System.Diagnostics.Debug.WriteLine("hello");
        }

        public void GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            System.Diagnostics.Debug.WriteLine("hello");
            ppvSite = new IntPtr();
        }
   }

}

I can't even get this to write to console. The BHO is being picked up by IE as I've registered it and it's appearing under "Manage Add-Ons" in the Settings menu.

Hopefully with this BHO I'm going to try access IE and then hopefully manipulate the DOM. It's an alien way of doing things for me (I'm used to Eclipse RCP, Linux) so I hope someone can point me in the right direction!

Cheers!

If you see BHO in the list of "Manage Add-Ons" page then it should be working. The operation System.Diagnostics.Debug.WriteLin likely won't work since BHOs are usually restricted to access everything beside the browser itself. Try another thing instead - try to paste method below into the methods GetSite/SetSite methods.

MessageBox.Show("Bho is working");

After that if message box is shown - they are invoked and BHO works. Also try to see what IE process are launched in the Task Manager, AFAIK BHO is allocated a separate process.

Regarding DOM manipulation - try samples from articles from CodeProject or other SO answers, eg from http://www.codeproject.com/Articles/149258/Inject-HTML-and-JavaScript-into-an-existing-page-w .

Hope that will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM