Win 10 IE 11-无法获得简单的BHO以在“增强保护模式”下运行

时间:2019-02-14 04:44:15

标签: c# internet-explorer bho

我正在尝试在IE 11-Win 10中以“增强保护模式”运行测试BHO。

BHO所做的只是发出警报,“ hello world”。

我已经使用“任何CPU”构建了该库。

我确保我已写入注册表并使其可写(请参见页面下方的完整课程):

var registryKey =
            Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);

        if (registryKey == null)
            registryKey = Registry.LocalMachine.CreateSubKey(
                                    BHO_REGISTRY_KEY_NAME, RegistryKeyPermissionCheck.Default);

我已将库放置在Program Files的子目录中。

我运行了一个.bat文件,该文件使用x32和x64 Regasm注册了该库。

当那行不通时,我尝试的另一种选择是专门为x32和x64构建该库。然后同时注册两者。

我花了很多时间对此进行研究(我在这里阅读了所有看起来与远程相关的文章),但我不明白自己在做什么错。

为了公平起见,我在这里找到的适用于Win 8和Win 7的可行解决方案。要明确地说,我无法使其在“ Win 10”环境下工作。我可以在“ Win 7”环境(我没有Win 8)上使用相同的代码。

我很拼命,任何帮助将不胜感激!

适用于任何CPU库的Install.bat:

@echo off

:: Register

SET SYSPATH=%SystemRoot%\Microsoft.NET\Framework\V4.0.30319\RegAsm.exe

%SYSPATH%  "%~dp0\IEExtension.dll"  /unregister

%SYSPATH%  "%~dp0\IEExtension.dll"  /codebase 

SET SYSPATH=%SystemRoot%\Microsoft.NET\Framework64\V4.0.30319\RegAsm.exe

%SYSPATH%  "%~dp0\IEExtension.dll"  /unregister 

%SYSPATH%  "%~dp0\IEExtension.dll"  /codebase

pause

我正在使用的测试bho扩展代码:

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

namespace TestExtension
{ 
    /* define the IObjectWithSite interface which the BHO class will implement.
        * The IObjectWithSite interface provides simple objects with a lightweight siting mechanism     (lighter than IOleObject).
        * Often, an object must communicate directly with a container site that is managing the object. 
        * Outside of IOleObject::SetClientSite, there is no generic means through which an object     becomes aware of its site. 
        * The IObjectWithSite interface provides a siting mechanism. This interface should only be used     when IOleObject is not already in use.
        * By using IObjectWithSite, a container can pass the IUnknown pointer of its site to the object     through SetSite. 
        * Callers can also get the latest site passed to SetSite by using GetSite.
        */
    [
        ComVisible(true),
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
        Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
    // Never EVER change this UUID!! It allows this BHO to find IE and attach to it
    ]
    public interface IObjectWithSite
    {
        [PreserveSig]
        int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);
    }

    /* The BHO site is the COM interface used to establish a communication.
        * Define a GUID attribute for your BHO as it will be used later on during
        * registration / installation
        */
    [
            ComVisible(true),
            Guid("3295D3BF-AEB3-4F89-96BC-8A83723B1388"),
            ClassInterface(ClassInterfaceType.None)
    ]
    public class TestBHO : TestExtension.IObjectWithSite
    {
        private WebBrowser webBrowser;
        public const string BHO_REGISTRY_KEY_NAME =
                "Software\\Microsoft\\Windows\\" +
                "CurrentVersion\\Explorer\\Browser Helper Objects";
        /* The SetSite() method is where the BHO is initialized and where you would perform all
            * the tasks that happen only once.
            * When you navigate to a URL with Internet Explorer, you should wait for a couple of events to make
            * sure the required document has been completely downloaded and then initialized. Only at this point
            * can you safely access its content through the exposed object model, if any. This means you need to
            * acquire a couple of pointers. The first one is the pointer to IWebBrowser2, the interface that
            * renders the WebBrowser object. The second pointer relates to events.
            * This module must register as an event listener with the browser in order to receive the
            * notification of downloads and document-specific events.
            */
        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)
        {
            var punk = Marshal.GetIUnknownForObject(webBrowser);
            var hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);
            return hr;
        }

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

                var head = (IHTMLElement)((IHTMLElementCollection)
                                    document.all.tags("head")).item(null, 0);

            var scriptObject =
                (IHTMLScriptElement)document.createElement("script");
            scriptObject.type = @"text/javascript";
            scriptObject.text = "alert('Hello World');"; // <---- JAVASCRIPT INJECTION HAPPENS HERE!

            ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);

        }

            /* The Register method simply tells IE which is the GUID of your extension so that it could be loaded.
            * The "No Explorer" value simply says that we don't want to be loaded by Windows Explorer.
            */
        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            var registryKey =
                Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);

            if (registryKey == null)
                registryKey = Registry.LocalMachine.CreateSubKey(
                                    BHO_REGISTRY_KEY_NAME, RegistryKeyPermissionCheck.Default);

            var guid = type.GUID.ToString("B");
            var ourKey = registryKey?.OpenSubKey(guid, true);

            if (ourKey == null)
            {
                ourKey = registryKey?.CreateSubKey(guid, RegistryKeyPermissionCheck.Default);
            }

            ourKey?.SetValue("NoExplorer", 1, RegistryValueKind.DWord);

            registryKey?.Close();
            ourKey?.Close();
        }

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

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

    } 
}

0 个答案:

没有答案