Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Getting C# BHO To Work

Status
Not open for further replies.

Khoramdin

Programmer
Nov 17, 2007
1
US
Hello everyone,

I have what should be a relatively simple C# .NET 2.0 IE BHO that I am really struggling with. The goal of the BHO is to provide a hook when the users opens a web-page the HelloWorld! message appears.

I am using Visual Studio 2005 Profesional Edition and do the following:

1- Build Solution (F6)
2- Fire an Internet Explorer (IE)
3- (From Visual Studio) Debug -> Attach to Process...
4- Select the Internet Explorer (IE) that was started in 1.
5- Attach

At this point I can see in the StatusBar loading message for verious files.

If I am not mistaken at this point my simple BHO should be attached to the Internet Explorer (IE). when I move from the webpage to other pages I expect for the HelloWorld MessageBox to pop-up once the website is loaded. But sadly, nothing happens!

The codes are:

IObjectWithSite.cs

using System;
using System.Runtime.InteropServices;

namespace WebWatcher
{
[ComVisible(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.Collections.Generic;
using System.Text;
using SHDocVw;
using mshtml;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace WebWatcher
{
[
ComVisible(true),
Guid("8a194578-81ea-4850-9911-13ba2d71efbd"),
ClassInterface(ClassInterfaceType.None)
]
public class BHO : IObjectWithSite
{
private SHDocVw.WebBrowser webBrowser;

public void OnDocumentComplete(object pDisp, ref object URL)
{
System.Windows.Forms.MessageBox.Show("HelloWorld!");
}

#region WebWatcher Internal Functions
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (SHDocVw.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)
{
IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);

return hr;
}
#endregion
}
}




How can I see what files are been loaded when I attach the codes to the Internet Explorer? Is there any particular setting that I should set the Visual Studio to inorder ro be able to get result? Your help is greatly appriciated. Thank you very much and have a great weekend.

Khoramdin
 
Hi,

Is your BHO properly registered on Explorer (Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects)?

If you already have, use System.Diagnostics.Debugger.Break() to debug the COM BHO.

By the way, your BHO will be used by both IE and Windows Explorer. So if your expecting it to load only in the web browser, you need to verify if the caller if it is IE (see Process.GetCurrentProcess).

my 2 cents
[wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top