Starting a Windows app from a windows service

A

Ahmed Perlom

Hi all,



I am trying to start a windows application that has a GUI from a Windows
service written in .NET 2.0. I have been searching on this for few days now
with no avail. When using the System.Diagnostic.Process object to start the
application (i.e Notepad), the new app runs and it is listed on the task
manger list, but the GUI doesn't show up in the desktop of the current user.
I am aware windows service (either LocalSystem, NetworkService or
LocalService) all run in a sandbox environment. I have read some where
here that there is no secure and easy way to do this, however I have no
option really and I wonder if some one could give me some hints as how would
I do this in C# or VB.NET!!.



Thanks
 
W

Willy Denoyette [MVP]

| Hi all,
|
|
|
| I am trying to start a windows application that has a GUI from a Windows
| service written in .NET 2.0. I have been searching on this for few days
now
| with no avail. When using the System.Diagnostic.Process object to start
the
| application (i.e Notepad), the new app runs and it is listed on the task
| manger list, but the GUI doesn't show up in the desktop of the current
user.
| I am aware windows service (either LocalSystem, NetworkService or
| LocalService) all run in a sandbox environment. I have read some where
| here that there is no secure and easy way to do this, however I have no
| option really and I wonder if some one could give me some hints as how
would
| I do this in C# or VB.NET!!.
|
|
|
| Thanks
|
|

Why do you need to start the UI application from a service in the first
place, there is no easy and secure way to do this. Why don't you start the
UI application when the user logs on, set a shortcut to the application in
the startup folder in the users profile and it will start whenever the user
logs on.


Willy.
 
A

Ahmed Perlom

Thanks of your reply. The idea is that the windows service would monitor the
run of that application. If the application halts, or of there is an update,
the service would ask all instances of the application to stop (assuming
multiple apps are running for each user session), then ask them to -restart
again. So the service has to be in a global location monitoring whenever
users log on/off. The other tough side is that it is the current design of
the solutions by the architect and I just joined in, not much I can do ;)
Please let me know about the hard and insecure way.

Thanx
 
D

David Levine

Make sure your service is enabled to interact with the active desktop - if
it isn't then the GUI portion may run on a desktop that is not visible to
the logged on user.
 
W

Willy Denoyette [MVP]

The OP is not talking about a Service with a GUI portion, he wants to start
another GUI style application from a service.
Note also that enabling a service to interact with a desktop won't work any
longer in VISTA and beyond.
Willy.


| Make sure your service is enabled to interact with the active desktop - if
| it isn't then the GUI portion may run on a desktop that is not visible to
| the logged on user.
|
|
| | > Hi all,
| >
| >
| >
| > I am trying to start a windows application that has a GUI from a Windows
| > service written in .NET 2.0. I have been searching on this for few days
| > now with no avail. When using the System.Diagnostic.Process object to
| > start the application (i.e Notepad), the new app runs and it is listed
on
| > the task manger list, but the GUI doesn't show up in the desktop of the
| > current user. I am aware windows service (either LocalSystem,
| > NetworkService or LocalService) all run in a sandbox environment. I
have
| > read some where here that there is no secure and easy way to do this,
| > however I have no option really and I wonder if some one could give me
| > some hints as how would I do this in C# or VB.NET!!.
| >
| >
| >
| > Thanks
| >
| >
|
|
 
W

Willy Denoyette [MVP]

I don't get it really, you are talking about multiple applications (Windows
Apps.) you want to start, stop, restart etc. What's the interactive user's
role here? Anyway, what you are trying to achieve is exactly what MSFT is
trying to prevent by all means, and is nearly impossible to make it secure
and robust. Note also that nothing in the framework will help you with this
either, you will have to PInvoke all Win32 API's and make sure you don't
forget any.
Before you go, you should have a good understanding of:
1. Windows desktops, window stations, how they interact with each other and
how they relate to the applications and their threads using these secured
executive objects. Please consult the MSDN docs and read as much as you can
about Desktops and WinStations (search MSDN for About Window Stations and
Desktops).
2. The security context of the user running as a service (the service
account) and the context of the application launched by such a service.

When launching an application from a service you will need to:
- Create a logon session by calling Win32 LogonUser (via PInvoke) followed
by,
- a call to LoadUserProfile, this is needed to load it's environment and
profile, when done you can call...
- CreateProcessAsUser, here you need to fill the STARTUPINFO structure and
especially you need to set the 'lpdesktop' member to "Winsta0\Default", by
doing this you will start the application in the visible desktop. But there
is a catch, before you call CreateProcessAsUser you will need to add the
Logon users SID to the DACL of the Winsta0 object. Failing to do this will
prevent the launched application to use the visible desktop.
With any luck you'll have your application running, but, remember that you
have created a new logon session, that means that the application doesn't
run in the already existing interactive logon session, so each time you do
this you will create a new session and you will load the Users Registry Hive
and the environmat block into that session as HKCU, this will consume a lot
of resources when the previous session did not terminate.

Willy.




| Thanks of your reply. The idea is that the windows service would monitor
the
| run of that application. If the application halts, or of there is an
update,
| the service would ask all instances of the application to stop (assuming
| multiple apps are running for each user session), then ask them
to -restart
| again. So the service has to be in a global location monitoring whenever
| users log on/off. The other tough side is that it is the current design
of
| the solutions by the architect and I just joined in, not much I can do ;)
| Please let me know about the hard and insecure way.
|
| Thanx
|
|
|
|
| | >
| > | > | Hi all,
| > |
| > |
| > |
| > | I am trying to start a windows application that has a GUI from a
Windows
| > | service written in .NET 2.0. I have been searching on this for few
days
| > now
| > | with no avail. When using the System.Diagnostic.Process object to
start
| > the
| > | application (i.e Notepad), the new app runs and it is listed on the
task
| > | manger list, but the GUI doesn't show up in the desktop of the current
| > user.
| > | I am aware windows service (either LocalSystem, NetworkService or
| > | LocalService) all run in a sandbox environment. I have read some
where
| > | here that there is no secure and easy way to do this, however I have
no
| > | option really and I wonder if some one could give me some hints as how
| > would
| > | I do this in C# or VB.NET!!.
| > |
| > |
| > |
| > | Thanks
| > |
| > |
| >
| > Why do you need to start the UI application from a service in the first
| > place, there is no easy and secure way to do this. Why don't you start
the
| > UI application when the user logs on, set a shortcut to the application
in
| > the startup folder in the users profile and it will start whenever the
| > user
| > logs on.
| >
| >
| > Willy.
| >
| >
|
|
 
J

John Timney \( MVP \)

I gotta agree with Willy on this one. Using a service like this is not a
good approach.

A cleaner alternative would be to create a monitor application, that starts
with the user - and checks the running processes. That way, it runs and
dies with that users and has all the correct rights to operate in that users
context/desktop. One feature of this is that you can add a socket
server/listener to it that accepts remote commands, allowing the app to
spawn localised applications easily at the active desktop. It will only be
a handful of code lines to create.
 
T

Terry

the way I have done this in the past is to have the service modify a
registry setting / file / etc when it needs to update teh UI app.
Have your UI application periodically check the registry key - if it is set,
then have the UI app shell another (very small, generic) app that takes an
exe as a command line parameter. The UI app then ends.
The 2nd app has a timer that wait 10 secs (to allow the first app to end)
and then shells the command line parameter passed - which is the name of the
first app to be restarted.
Consideration must be given for the case when the user is currently in the
middle of doing something - if the app dies then you shall not thanked.
cheers
Terry
 

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