Home > Uncategorized > Browser Helper Objects C#

Browser Helper Objects C#

Can anybody help tell my why this code isn’t working. Its a Browser Helper Object for Internet Explorer, Simply designed to Write the Current Time at the bottom of each page. It compiles ok, and it registers as a COM object with regasm ok, and I put its GUID in the correct place in the registry (under Explorer/Browser Helper Objects). I can’t even get VS.NET to attach to the IEXPLORE process.

using System;

using

SHDocVw;

using

mshtml;

namespace BrowserHelperObject

{

using System;

using System.Runtime.InteropServices;

[ComVisible(true),

InterfaceType(ComInterfaceType.InterfaceIsIUnknown),

Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")]

public interface IObjectWithSite

{

void SetSite ([MarshalAs(UnmanagedType.IUnknown)]object site);

void GetSite (ref Guid guid, out object ppvSite);

}

/// <summary>

/// Summary description for CustomBrowserHelperObject

/// </summary>

[ComVisible(

true),

ClassInterface(ClassInterfaceType.None),

Guid("9E6F3590-52D0-406d-A7ED-DAE87BED989F")]

public class CustomBrowserHelperObject : IObjectWithSite

{

const int E_FAIL = unchecked((int)0x80004005);

const int E_NOINTERFACE = unchecked((int)0x80004002);

private IWebBrowser2 iwb2InternetExplorer;

public void SetSite ([MarshalAs(UnmanagedType.IUnknown)]object site)

{

iwb2InternetExplorer = (IWebBrowser2)site;

if (iwb2InternetExplorer != null)

{

WebBrowser wbInternetExplorer = (WebBrowser)iwb2InternetExplorer;

wbInternetExplorer.DocumentComplete +=

new DWebBrowserEvents2_DocumentCompleteEventHandler(

this.OnDocumentComplete);

}

}

public void GetSite (ref Guid guid, out object ppvSite)

{

ppvSite=

null;

if (iwb2InternetExplorer != null)

{

IntPtr ipSite = IntPtr.Zero;

IntPtr ipUnknown = Marshal.GetIUnknownForObject(iwb2InternetExplorer);

Marshal.QueryInterface(ipUnknown, ref guid, out ipSite);

Marshal.Release(ipUnknown);

Marshal.Release(ipUnknown);

if (!ipSite.Equals(IntPtr.Zero))

{

ppvSite = ipSite;

}

else

{

Release();

Marshal.ThrowExceptionForHR(E_NOINTERFACE);

}

}

else

{

Release();

Marshal.ThrowExceptionForHR(E_FAIL);

}

}

private void OnDocumentComplete (object frame, ref object urlObj)

{

HTMLDocument hDoc = (HTMLDocument)iwb2InternetExplorer.Document;

hDoc.body.innerHTML = hDoc.body.innerHTML + "<hr><br>Page viewed at " + DateTime.Now;

}

protected void Release()

{

if (iwb2InternetExplorer != null)

{

Marshal.ReleaseComObject(iwb2InternetExplorer);

iwb2InternetExplorer = null;

}

}

}

}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: