본문 바로가기
IT-개발,DB

[C#] best practice is to write a BHO which will load your BandObject.

by SB리치퍼슨 2010. 9. 10.

best practice is to write a BHO which will load your BandObject.

A BHO has to implement IObjectWithSite

--- IObjectWithSite.cs ---

using System;
using
System.Runtime.InteropServices;
namespace
<YOUR_NAMESPACE_HERE>
{
   [C
omVisible(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);
  
}
}

--- BHO.cs ---

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using SHDocVw;

namespace <YOUR_NAMESPACE_HERE>
{

   [ComVisible(true)]
   [
Guid("<YOUR_GUID_HERE>")]
   [ClassInterface(ClassInterfaceType.None)]

   public class BHO : IObjectWithSite
  
{
     
private InternetExplorer explorer;
    

      #region ComRegisterFunction
     
public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects"

      [ComRegisterFunction]
     
public static void RegisterBHO(Type t)
      {
        
RegistryKey key = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
         if (key == null)
         
{
           
key = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
        
}

         string guidString = t.GUID.ToString("B");
        
RegistryKey bhoKey = key.OpenSubKey(guidString, true);

         if (bhoKey == null)
         {
            bhoKey = key.CreateSubKey(guidString);
         }

         // NoExplorer:dword = 1 prevents the BHO to be loaded by Explorer
        
string _name = "NoExplorer";
        
object _value = (object)1;
         bhoKey.SetValue(_name, _value);
         key.Close();
         bhoKey.Close();
      }

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

         if (key != null)
         {
            key.DeleteSubKey(guidString,
false);
         }
      }

      #endregion

      #region IObjectWithSite Members
      public int SetSite(object site)
      {
        
if (site != null)
         {
            explorer = (
InternetExplorer)site;
            ShowBrowserBar(
true);
         }
      
   return 0;
      }

      public int GetSite(ref Guid guid, out IntPtr ppvSite)
      {
        
IntPtr punk = Marshal.GetIUnknownForObject(explorer);
        
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
         
Marshal.Release(punk);
        
return hr;
      }

      #endregion

      #region Private Functions

      private void ShowBrowserBar(bool bShow)
      {
         
object pvaClsid = (object)(new Guid("GUID_OF_YOUR_BANDOBJECT_HERE").ToString("B"));
        
object pvarShow = (object)bShow;
        
object pvarSize = null;
        
        
if (bShow) /* hide Browser bar before showing to prevent erroneous behavior of IE*/
        
{
           
object pvarShowFalse = (object)false;
            explorer.ShowBrowserBar(
ref pvaClsid, ref pvarShowFalse, ref pvarSize);
         }

         explorer.ShowBrowserBar(ref pvaClsid, ref pvarShow, ref pvarSize);
      }

      #endregion

   }
}

반응형

댓글