views:

325

answers:

3

How can I start a program in an active user session from a running .NET background process. Basically I have a .NET application running as a service and I want that application to launch a specific windowed application on the users desktop when certain events happen.

How can I do this?

This question is related to this one: http://stackoverflow.com/questions/1727828/start-a-program-in-active-user-session-with-powershell-remoting

Update
Using Process.Start alone will not work here since it launches the application in the current application session. It doesn't launch the application in a specific users active desktop session.

A: 

The CreateProcessWithLogonW function will allow you to pass in the credentials of the user you want the application to run as.

Also, note that if you go down this route you do need to pass in the STARTUPINFO structure so that it starts on the right desktop, and not just leave it null.

Lee Meyers
"Run As" is not the same as what I am trying to do here. It just runs an application in the current session using a specific users credentials.
spoon16
Sorry about that, I didn't check your related question first. I believe it would be possible to achieve on a single machine by using WTSQueryUserToken in order to launch in a particular session, but I didn't realise that remoting was involved.
Lee Meyers
+2  A: 

Is it feasible for you to build some kind of lightweight listener application that the user would run in the background? Presumably your service could communicate with the listener and have the listener instantiate the windowed app since it's running in the user's context.

Tom Crowe
Definitely a possible approach. Not very interesting to me though. I am just looking for a quick way to recycle an application on my own session across multiple machines using Windows Remoting (.NET). If I can't do it easily, than the best thing to do is just make the app run as a service and I can restart the service and not worry about it being run on a specific sessions desktop.
spoon16
A: 

Look into the Log On context of the windows service as shown here. This allows interaction of the windows service with a desktop session.

BlueSam
I believe this only works on the /CONSOLE session though and doesn't allow you to target a specific active user session.
spoon16
I believe you are incorrect as I have seen a windows service interact with the user's desktop before.
BlueSam