本帖最后由 global_biz 于 2014-06-19 16:27:44 编辑

解决方案 »

  1.   

    可能程序没执行成功把System.Windows.Forms.MessageBox.Show("BHO TESTING.2..");放到ourKey.Close();后面看看
      

  2.   


    我重新改了下弹出框,加多了输出,然后,测试的时候,是输出下面结果:弹出框 2,6,8,10,11,12,13 这几个弹出框都弹出来,然后, cmd 提示注册成功.C:\Users\Administrator>"C:\Program Files (x86)\Test Program\tools\x86\RegAsm.exe
    " /codebase "D:\TestBHO.dll"
    Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.5420
    Copyright (C) Microsoft Corporation 1998-2004.  All rights reserved.RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can ca
    use your assembly to interfere with other applications that may be installed on
    the same computer. The /codebase switch is intended to be used only with signed
    assemblies. Please give your assembly a strong name and re-register it.
    Types registered successfully以下是修改后的代码:public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";[ComRegisterFunction]
    public static void RegisterBHO(Type type)
    {
        System.Windows.Forms.MessageBox.Show("BHO TESTING.2..");
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);    if (registryKey == null)
        {
            System.Windows.Forms.MessageBox.Show("BHO TESTING.4..");
            registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
        }    System.Windows.Forms.MessageBox.Show("BHO TESTING.6.." + BHOKEYNAME);    string guid = type.GUID.ToString("B");
        RegistryKey ourKey = registryKey.OpenSubKey(guid);    System.Windows.Forms.MessageBox.Show("BHO TESTING.8.." + guid);    if (ourKey == null)
        {
            System.Windows.Forms.MessageBox.Show("BHO TESTING.10.." + guid);
            ourKey = registryKey.CreateSubKey(guid);
            System.Windows.Forms.MessageBox.Show("BHO TESTING.11.." + ourKey);
        }    ourKey.SetValue("Alright", 1);
        System.Windows.Forms.MessageBox.Show("BHO TESTING.12.." + ourKey);    registryKey.Close();
        ourKey.Close();    System.Windows.Forms.MessageBox.Show("BHO TESTING.13..");
    }[ComUnregisterFunction]
    public static void UnregisterBHO(Type type)
    {
        System.Windows.Forms.MessageBox.Show("BHO TESTING..3.");
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
        string guid = type.GUID.ToString("B");    System.Windows.Forms.MessageBox.Show("BHO TESTING.5.." + guid);    if (registryKey != null)
        {
            registryKey.DeleteSubKey(guid, false);
            System.Windows.Forms.MessageBox.Show("BHO TESTING.7.." + guid);
        }
        System.Windows.Forms.MessageBox.Show("BHO TESTING.9.." + guid);
    }
      

  3.   


    然后, 可能是因为注册表的键值也没有, 所以, 当我打开 IE 测试 BHO 的时候, 好像根本就没反应, 下面的代码没有在页面加载完成的时候被调用:没有弹出: System.Windows.Forms.MessageBox.Show("BHO TESTING...");
    public class BHO : IObjectWithSite
    {
        WebBrowser browser;
        HTMLDocument document;    public void OnDocumentComplete(object pDisp, ref object URL)
        {
            System.Windows.Forms.MessageBox.Show("BHO TESTING...");
        }    public int SetSite(object site)
        {
            if (site != null)
            {
                browser = (WebBrowser)site;
                browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
            }
            else
            {
                browser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
                browser = null;
            }
            return 0;
        }    public int GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(browser);
            int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);
            return hr;
        }
    .....
      

  4.   


    我重新将上面的 dll 放到 windows server 2003 上面运行, 同样的结果却成功注册与成功在 IE 运行 BHO. 这个问题是与 OS 的版本有关,谢谢.