2

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: 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.

Community
  • 1
  • 1
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
  • I wasn't 100% sure it would work and almost didn't post the answer. – ChrisF Nov 13 '09 at 16:25
  • No worries Chris, I appreciate your time. – Eric Schoonover Nov 13 '09 at 16:36
  • 1) What would happen if there are multiple people logged in to the system? This can happen with Terminal Services or Fast User Switching. 2) Do you want the windowed application to be launched in the user's security context or the service's security context? – Ragesh Nov 13 '09 at 17:00
  • I want to be able to target it at a specific user on a specific machine and I want to be able to detect the state of the session. It's not really that big of a deal if the user is not active or doesn't see it right away just so long as the next time they connect to the server the dialog is up. If they get logged off or their session gets killed it's ok if the message doesn't appear next time they login. – Eric Schoonover Nov 13 '09 at 17:34
  • I'm guessing you've found a solution to this by now (since the question is >2 years old), if you haven't then I have a solution for this problem that I can post as an answer... – M.Babcock Mar 17 '12 at 23:00
  • post it, I don't need the solution anymore, but others may – Eric Schoonover Mar 25 '12 at 00:42

3 Answers3

2

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
  • 486
  • 4
  • 3
  • 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. – Eric Schoonover Nov 13 '09 at 17:37
0

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
  • 39
  • 3
  • 1
    "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. – Eric Schoonover Nov 13 '09 at 16:37
  • 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 Nov 13 '09 at 18:03
0

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
  • 1,888
  • 10
  • 17