473,513 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.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 1925
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2236
by: Michael Riggio | last post by:
Is there a way to have a windows service instantiate a class that is a web service, which will then be accessible to clients via HTTP? Thanks, -Mike
1
5657
by: Artur Kowalski | last post by:
I have a NotifyIcon in my Windows Service project and I am trying to add a ContextMenu to this NotifyIcon or use some of the mouse events. Everything isn't working. I think so base class of the...
9
7232
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
1
3435
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to work, can anyone shed some light as to where I'm...
0
3906
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to do the following: - Select remote server -...
3
3668
by: Doug Bailey | last post by:
I am trying to control a Windows Service via a Web Service interface. (I am developing in .NET2003) I am using the ServiceController object which allows me to read the state of the services with...
2
6879
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
4
4159
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
5
3271
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
1
2069
by: =?Utf-8?B?TWFuanJlZSBHYXJn?= | last post by:
Hi, I created a web service that I want to host in windows service. The problem is that if I host it as windows service it does not use the configuration file. I have to define the binding,...
0
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.