Problem connecting an event handler to IE in a Windows service

S

SunshineGirl

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
"shellWindows = new SHDocVw.ShellWindowsClass();":

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.ServiceProcess.ServiceBase
{
private System.ComponentModel.Container components = null;
private static SHDocVw.ShellWindows shellWindows = null; //<=====

public Service1()
{
InitializeComponent();
}

static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);

if(!EventLog.SourceExists("Service1"))
{
EventLog.CreateEventSource("Service1", "Application");
}
}

private void InitializeComponent()
{
this.ServiceName = "Service1";
}

protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

protected override void OnStart(string[] args)
{
EventLog.WriteEntry("Service1", "0.This is just a test.",
EventLogEntryType.Information);

shellWindows = new SHDocVw.ShellWindowsClass(); //<=====
foreach(SHDocVw.InternetExplorer ie in shellWindows) //<=====
ie.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(this.ie_BeforeNavigat
e2); //<=====
}

public void ie_BeforeNavigate2(object disp, ref object url, ref
object flags, ref object targetFrameName, ref object postData, ref object
headers, ref bool cancel) //<=====
{
EventLog.WriteEntry("Service1", "ie_BeforeNavigate2.",
EventLogEntryType.Information); //<=====
}

protected override void OnStop()
{
}
}
}
 
T

Tian Min Huang

Hello,

Thanks for your post. As I understand, the problem you are facing is that
it fails to enumerate existing IE windows from a Windows Service. Please
correct me if there is any misunderstanding. I now share the following
information with you:

Based on my experience, I believe the reason of the problem is that a
Windows Service is running in a hidden window station and desktop other
than "WinSta0\Default". To work around this problem, you may have to set
your service as an interactive service b the following steps:

1. Go to "Control Panel" -> "Service".
2. Select the service in list box and click "Startup" button.
3. Select "System Account" and check "Allow Service to Interact with
Desktop".

I belive the following article is helpful:
Interacting with the User in a Service
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/interacting_with_the_user_in_a_service.asp

I look forward to your result.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top