천객만래 [千客萬來] (It has an interminable succession of visitors)

'IObjectWithSite'에 해당되는 글 1건

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

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

   }
}

Posted by SB패밀리