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
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