views:

142

answers:

3

I am trying to make a service that spawns a desktop application, and then watches to make sure it restarts again if it is closed. .

I would like it to basically spawn the process and then forget about it, allowing to act like a normal interactive application. (Apparently this is much easier to do in XP and before, but I need this for XP, Vista, and 7)

My problem now is that either it shows up invisible if I use process.start() with desktop interactive checked, and if I directly spawn a form it asks "Do you REALLY want to do this?!" and then the whole screen goes blank EXCEPT for my program.

I just want this to be an inoffensive background app. I have the app working well, I just need to figure out how to spawn it from a service without all the trouble.

I am finding all of this stuff that says "Don't make services that have UI", but first off this was a requirement that was given to me. (Boss does not want it to be a scheduled task) Also, I noticed that the Task scheduler is itself a service, and it does not have any problem spawning user interactive applications. Why can't I do that too?

What am I doing wrong?

UPDATE: Thanks for all the useful info. Was able to convince my boss that this wasn't possible to achieve based on the session 0 isolation, so I marked that one as the answer and gave upvotes to everyone who gave useful info. Thanks again! :)

A: 

Can't you make a program that is start at the login of the user and that will keep running until the session is closed ? I'd do that if I were you. You could start it with the login script of your users.

One problem with using services here is that they run without the user info and it also runs when nobody is logged on, hence the reason not to use UI in services.

David Brunelle
+1  A: 

As of Windows Vista (and consequently Windows 7), services cannot directly interact with the user/desktop for security reasons. Dialogs and other forms that are spawned from a service are shown in Session 0 while your users are logged into Session 1. For more on this, read the Session 0 Isolation section here. In short, calling Process.Start() from a Windows service on Windows 7 is going to cause issues.

You can still interact with the user. You just have to do so indirectly, as described here. I hope that points you in the right direction.

Matt Davis
Thank you for the links.Unfortunately, I need more than just a dialog box, so WTSSENDMESSAGE won't work, and both your links imply that the CREATEPROCESS as user won't work on Vista or later. Is this correct?
Brandi
@Brandi, as I understand it, it's not that CREATEPROCESS doesn't work. It's that it spawns the process as part of Session 0. Your user won't see the app because she's logged into Session 1 or higher.
Matt Davis
@Brandi, I think you'll actually need to use the CreateProcessAsUser approach. See this link for more details on how to do it. http://social.msdn.microsoft.com/Forums/en/windowssecurity/thread/31bfa13d-982b-4b1a-bff3-2761ade5214f
Matt Davis
@Matt: Yeah, it does. If I launch the app directly it essentially transports me to Session 0 I think, which isn't what I want. CreateProcessAsUser I am wary of because it requires a plain text password first off (and I need to make this on lots of computers), besides which, even if I made a generic user for it to use, how do I send the application to the correct user? I'm not so great at C++, but if you have any examples that you think would work for my scenario, I'd love to try it out. :)
Brandi
+1  A: 

I would push back on why "Boss does not want it to be a scheduled task". You can set up a scheduled task that runs on every login, for example, which sounds like it's exactly what you need. You could also catch the Closing event and (from code) set up another scheduled task to relaunch yourself a few moments later.

What the boss probably doesn't get is that services are special. For example they are exempt from UAC. Therefore they can't talk to the user. If you want something that walks and talks like a service, but also talks to the user, the Windows 7 word for that is scheduled task.

Kate Gregory
Haha, this is what I ended up doing.
Brandi