I have a Windows service(C#) which will be kicked off by an app(.exe). Since we need Admin privileges to start/Stop/Query status of the service. we are running this app in Admin mode.
But, we want to make it to run in 'asInvoker' mode. thus made me think of possibility of start/stop or query status of the Windows Service from an app(.exe) running in asInvoker(Basically non-Admin) Mode. Is it possible to tweak the windows service that way?
Any other constructive solutions are most welcome.
Related
My problem: Can I run an exe (a Windows Forms application) from a Windows service? If so, how?
You should be able to use System.Diagnostics.Process.Start http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx to start an executable process.
But please keep in mind that services are expected (and most often does) run in their own context. For example the service would start running as soon as windows startes and even before any user is logged on. If your service decides to start the exe when no one is logged in, you will not see the UI of the form
First for all you should say what versions of windows, for vista and later here there article, for xp you should check "allow desktop interaction" in service property, but anyway it's considered as bad and insecure practice, so by any chance you should try to avoid that.
I have a Windows Service from which I have to run a process. However, that process requires admin privileges to run. Inside Process Explorer, I see that the service does run the process but the UI for that process does not appear up.
What am I missing here? How can this be rectified?
The services run in a separate session, with a separate desktop. Think of it as if the services where running in another remote desktop session. Anything with a GUI that you start from within a service will be shown in that hidden session, which cannot be accessed.
If you're working on Windows Vista or 7, services can't interact with desktop like windows XP and they can't open a window like ordinary windows applications. They default run as Local System account which is a fully privileged account.
I have a bat file which is installed as a service. I can run the service on a remote box. This service needs to launch another application. The launched application needs to be visible(run in foreground). Currently the launched application is running in background as the windows service is running in background. How can i make the windows service or both the windows service and the application that it launches run in foreground? I intend to manage the service with the ServiceController class in C#.
I think you should probably read
http://asprosys.blogspot.com.au/2009/03/allow-service-to-interact-with-desktop.html
Making a service to just launch another app is a real security issue, what if your other app gets replaced with something else do you have all the checks in your Service to ensure your app is the app you think it is.
A proper approach is to launch the second application in the user's session, while your Windows service app always runs in session 0.
The approach has been part of my discussion with #RaheelKhan under this thread,
How reliable is adding an application manifest to ensure elevated privileges across windows xp, vista and 7?
It requires proper understanding of Windows sessions, session isolation, and platform invoke.
I created a server application that always need to be online and running.
In case that my application is been shutdown i want to restart the application.
Is there a way that "Windows service" will be my online insurance?
If not is there another way?
Thanks.
Yes, windows services can be set to restart if they fail.
In case that my application is been shutdown i want to restart the application.
This makes NO Sense and you should not do it.
If the admin shuts you down (and that is the only way a service is shut down) then he has a reason - maybe he has to apply emergency patches or something else.
If the app CRASHES that is another thing (and windows services can be set up to automatically restart when crashing), but if you geta shutdown request do NOT restart as a service. Never. I can name you a dozen cases where it would be stupid - mostly around system maintenance that you would interfere with. You basically risk being terminated because the computer is switched off.
If your application is not a windows service you need to learn the basics how windows works, because it is ridiculously crappy to have a normal application act as sever process. For anything, a user must be interactively logged in. Next time I see a server with a logged in user because some stupid programmer could not do his job I promise I (censored).
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:
We don't wait for an interactive user logon and the Application falls because it tries and fails to initialize DirectX device.
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.
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.
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.
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.
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.