471,310 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

.net windows service

NG
Hi All
I made a windows service in C#. Now I have a situation where I need to
invoke an exe with some UI. Assuming that the computer running this service
will always be logged in, can anyone please provide me a sample code to
invoke an exe from windows service in .net.
I tried a few methods that I found on the net but they seem to invoke
something in the background which actually does not work.
Any help is much appreciated.
Thanks in advance.
Best regards,
Nitesh Gupta
Dec 1 '05 #1
13 1843
KJ
You already tried the Process class? Perhaps you merely need to change
the ServiceController.ServiceType to 256 (InteractiveProcess)?

Dec 1 '05 #2
NG
Thanks for the reply KJ.
Yes, I used Process class..

I did this::
System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName="notepad.exe"; // I have a different program to be run

bool x=p.Start();
Could you please give me a sample code snippet for the
ServiceController.ServiceType that you mentioned. A little guideline/hint
might also help me to get me started on the right path. I am not sure where
to set that property you mentioned.

Thanks,
Nitesh
"KJ" <n_**********@mail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
You already tried the Process class? Perhaps you merely need to change
the ServiceController.ServiceType to 256 (InteractiveProcess)?

Dec 1 '05 #3
NG
I am able to achieve this if I check the option "Allow service to interact
with desktop" in service properties from MMC. Not sure how to set this
option programmatically.
Dec 1 '05 #4
You can set it at installation time. MSI allow you to set it.
To set by .NET source code, you will need to use WMI (System.Management
namespace), or invoke Windows API.

Note that ServiceController.ServiceType is readonly. And the
System.Configuration.Install.Installer you add for your service (which
can be used with installutil.exe) does not support this property.

Thi

Dec 1 '05 #5
On Thu, 1 Dec 2005 15:10:27 +0530, NG wrote:
I am able to achieve this if I check the option "Allow service to interact
with desktop" in service properties from MMC. Not sure how to set this
option programmatically.


Be carefull when starting a UI application from a Windows Service. Unless
your Windows Service is running under a limited Windows Account (which is
not the case by default), starting a UI appplication from the service means
giving Administrator rights to potentially anybody using the machine (even
if the users are logged in a limited Windows account).
Dec 1 '05 #6

"NG" <xgniteshATyahooDOTcom> wrote in message
news:eW**************@TK2MSFTNGP12.phx.gbl...
I am able to achieve this if I check the option "Allow service to interact
with desktop" in service properties from MMC. Not sure how to set this
option programmatically.

Use this option only for debugging, Windows Services should otherwise never
interact with the logged-on user.
Note that "Interact with desktop" services will not work in Vista.

Willy.
Dec 1 '05 #7
KJ
Hi Nitesh, I do have code to do this. Actually, as I remember it is
either a win32 call or a registry change call. I will post it tonight
(12/1/05)

Dec 1 '05 #8
KJ
Nitesh,

As promised, here is the code.

This code is written in the ServiceInstaller class (subclass of
System.Configuration.Install.Installer), and the method to make it
interactive is called in the constructor. Hope this helps.

public EnactmentServiceInstaller()
{
// This call is required by the Designer.
InitializeComponent();
try {
if
(bool.Parse(ConfigurationSettings.AppSettings["InteractiveModeInstall"].ToString()))
{
SetInteractiveBool();
}
} catch (Exception exp) {
Trace.WriteLine(exp);
}
}

private void SetInteractiveBool() {
// Here is where we set the bit on the value in the registry.
// Grab the subkey to our service
RegistryKey ckey = Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Services\" +
StringEnums.EnactmentWinServiceName, true);
// Good to always do error checking!
if(ckey != null) {
// Ok now lets make sure the "Type" value is there,
//and then do our bitwise operation on it.
if(ckey.GetValue("Type") != null) {
ckey.SetValue("Type", ((int)ckey.GetValue("Type") | 256));
}
}
}

Dec 2 '05 #9
NG
Thanks to all of you for your valued suggestions. Thanks KJ for the code
sample. It helps me to proceed in the right direction.
Best regards,
Nitesh
Dec 2 '05 #10
NG
I have been able invoke an application from windows service. I am stuck
with another problem.
The program that was invoked was actually downloading a web-page using the
web-browser control. This was working perfectly when this app was used as
an standalone application run from a command prompt. But when called from
the service, the web-browser control shows another error page and not the
actual web page that was requested. The contenets of the error page shows
the following error:
Internet Explorer was unable to link to the web page you requested. The
page might be temporarily unavailable.......

This happens with all web-sites I tried.

I found on the net that this error occurs when the site has been requested
and then the request is cancelled immideately.

I am not sure but this has something to do with windows service security?
Is the service making the download to stop?

Any help is appreciated on this.
Thanks,
Nitesh Gupta


Dec 2 '05 #11
KJ
You could always try setting the service properties to run as a domain
or local user (not localservice)

Dec 2 '05 #12

"NG" <xgniteshATyahooDOTcom> wrote in message
news:eS*************@TK2MSFTNGP15.phx.gbl...
I have been able invoke an application from windows service. I am stuck
with another problem.
The program that was invoked was actually downloading a web-page using the
web-browser control. This was working perfectly when this app was used as
an standalone application run from a command prompt. But when called from
the service, the web-browser control shows another error page and not the
actual web page that was requested. The contenets of the error page shows
the following error:
Internet Explorer was unable to link to the web page you requested. The
page might be temporarily unavailable.......

This happens with all web-sites I tried.

I found on the net that this error occurs when the site has been requested
and then the request is cancelled immideately.

I am not sure but this has something to do with windows service security?
Is the service making the download to stop?

Any help is appreciated on this.
Thanks,
Nitesh Gupta


As I said before, only set this for debugging purposses, services are not
designed to interact with the interactive desktop, nor should they start
interactive applications. Services run in a special security context, you
break the security by interacting with the desktop, but you only "took" one
of the many hurdles waiting for you.
The problem you are facing now is that you don't have access to an
interactive user profile (HKCU), services don't run in an interactive logon
session. One last advise, start you GUI application from a regular logon
session and forget the service.
Willy.

Dec 2 '05 #13
NG wrote:
Hi All
I made a windows service in C#. Now I have a situation where I need
to invoke an exe with some UI. Assuming that the computer running
this service will always be logged in, can anyone please provide me a
sample code to invoke an exe from windows service in .net.
I tried a few methods that I found on the net but they seem to invoke
something in the background which actually does not work.
Any help is much appreciated.


The advice that Willy gives is spot on: you should not run a service to
interact with the desktop, nor should you even consider starting the UI
process from the service's account because the privileges of the
service's account will not be appropriate.

Instead, you should access the interactive user's window station and
make the thread impersonate the interactive user's account and then
create the process under that impersonation token. There is no simple
way to do that in .NET, in fact there is no simple way to do that in
Win32 either, although Keith Brown's excellent book "Programming Windows
Security" has all of the details.

A better solution is to create a separate process - possibly without a
UI, and only a notification icon in the tray - that is created at
interactive logon using the StartUp folder or the Run tregistry entry.
This would be a socket server, or a .NET remoting server and the service
can then send messages to this process to tell it to start processes.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Dec 4 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Michael Riggio | last post: by
1 post views Thread by Artur Kowalski | last post: by
9 posts views Thread by SP | last post: by
1 post views Thread by Scott Davies | last post: by
reply views Thread by Scott Davies | last post: by
3 posts views Thread by Doug Bailey | last post: by
4 posts views Thread by tshad | last post: by
5 posts views Thread by dm3281 | last post: by
1 post views Thread by =?Utf-8?B?TWFuanJlZSBHYXJn?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.