I'm trying to connect an event handler to the BeforeNavigate2 event of
Internet Explorer. This works in a Windows app, but I can't get it to work
in a Windows service. I get the following exception when I run the line
"shellWindo ws = new SHDocVw.ShellWi ndowsClass();":
COM object with CLSID {9BA05972-F6A8-11CF-A442-00A0C90A8F39} is either not
valid or not registered.
I know that this CLSID belongs to the ShellWindows interface.
Any ideas?
Here's the source code:
namespace WindowsService1
{
public class Service1 : System.ServiceP rocess.ServiceB ase
{
private System.Componen tModel.Containe r components = null;
private static SHDocVw.ShellWi ndows shellWindows = null; //<=====
public Service1()
{
InitializeCompo nent();
}
static void Main()
{
System.ServiceP rocess.ServiceB ase[] ServicesToRun;
ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new
Service1() };
System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
if(!EventLog.So urceExists("Ser vice1"))
{
EventLog.Create EventSource("Se rvice1", "Applicatio n");
}
}
private void InitializeCompo nent()
{
this.ServiceNam e = "Service1";
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose(di sposing);
}
protected override void OnStart(string[] args)
{
EventLog.WriteE ntry("Service1" , "0.This is just a test.",
EventLogEntryTy pe.Information) ;
shellWindows = new SHDocVw.ShellWi ndowsClass(); //<=====
foreach(SHDocVw .InternetExplor er ie in shellWindows) //<=====
ie.BeforeNaviga te2 += new
SHDocVw.DWebBro wserEvents2_Bef oreNavigate2Eve ntHandler(this. ie_BeforeNaviga t
e2); //<=====
}
public void ie_BeforeNaviga te2(object disp, ref object url, ref
object flags, ref object targetFrameName , ref object postData, ref object
headers, ref bool cancel) //<=====
{
EventLog.WriteE ntry("Service1" , "ie_BeforeNavig ate2.",
EventLogEntryTy pe.Information) ; //<=====
}
protected override void OnStop()
{
}
}
}