2

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.

7
  • If you want to launch an application with the permissions of a user when they log on, what's wrong with one of the standard autorun options (either a registry key or the Startup folder)? Commented Mar 1, 2011 at 20:31
  • @Zooba, one of winservice task is to looking after Application after start and restart it when it falls. Commented Mar 1, 2011 at 20:34
  • @acoolaum Windows provides some of this functionality itself (msdn.microsoft.com/en-us/library/cc303708.aspx), which may be an option, or can the application notify the service when it starts up to achieve the same effect? Commented Mar 1, 2011 at 20:44
  • You can register it with the restart manager, with the Windows API Code Pack. Commented Mar 1, 2011 at 20:47
  • @Zoomba, good article but all software runs on remote machines and high requirements of reliability must applied to it. Therefore I can't use Application Restart due "WER will not restart an application if it executed for less than 60 seconds before being terminated". Commented Mar 2, 2011 at 5:17

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, I'll read it but I think it may resolves problem number two only.
Most likely, because the other two problems are not really ones that services are for.
why not? The main target of my WinService is critical application health monitoring and to provide remote control to computer.
Services are not really meant to interact with the desktop and in Vista+ it's basically a non-starter (are you sure your XP one worked perfectly after a user logs off and logs back on? Because then they're apt to be on a different desktop from the service).
1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.