acoolaum
acoolaum

Reputation: 2130

Launch program under interactive logon user from .NET WinService

At the moment we have .NET WinService started under LocalService user at windows start. The service launch another WinForms Application using Process.Start().

But there are several problems in this solution:

  1. We don't wait for an interactive user logon and the Application falls because it tries and fails to initialize DirectX device.
  2. Application launched under LocalService perfectly interacts with user desktop in Windows XP. But it doesn't work in Windows 7 because of there are different graphic stations for each user in win7.
  3. Sometimes we need to run application with current interactive logon user rights.

Does anybody know how to wait for user interactive logon in the service and start WinForms Application with these user rights?

I think this helps to solve all problems.

Upvotes: 2

Views: 1648

Answers (3)

Mark Sowul
Mark Sowul

Reputation: 10600

You will need a separate client app. Check out this document, page 6: http://msdn.microsoft.com/en-us/windows/hardware/gg463353.aspx.

For your monitoring/restart scenario look at CreateProcessAsUser as mentioned in the document. You will almost certainly need to have your client app coordinate with the service for this, and it's still pushing a square peg into a round hole.

Upvotes: 2

Jason Weber
Jason Weber

Reputation: 1522

Is it possible that this just isn't the right approach for what you're trying to do? It seems possible that you'd be better off putting the monitoring logic or whatever has the uptime requirements into the service so that it's "always on" so to speak. Then you would be left with UI logic in the WinForms app, which could be open or closed with no ill effect.

Upvotes: 1

Jonas
Jonas

Reputation: 678

I would try using a combination of the answers above.

To solve #1 At user logon, launch the Winforms application using autostart in registry or startup folder. Make it notify the service that it was started successfully.

To make sure that the Winform app is started successfully after user log on: Have your service that checks if application is started running in the background as you have now but don't let it do the initial startup. Instead just let it register when user logs on, should be possible to do by listening to OnSessionChange. Set a delay for X number of seconds to allow the login/startup process finish before it starts checking for a running application (ok maybe not the best solution).

If the service discovers that the application is not started or crashes, restart it from the service using the method Mark points out, CreateProcessAsUser.

Upvotes: 1

Related Questions