By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

Problem connecting an event handler to IE in a Windows service

P: n/a
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(ServicesToRu n);

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_BeforeNavigate2EventHan dler(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()
{
}
}
}
Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/de...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.

Nov 22 '05 #2

P: n/a
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/de...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.

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.