SetServiceStatus() when running in debug mode. This is made easier by the fact that we have a service base class that passes the derived class (which is where each particular service's code lives) an interface for it to use to notify the SCM of things. Our derived class needn't know that we're running in debug mode because it never calls into the SCM directly, it calls via the interface that it's been given and we can simply decide to ignore the calls when running in debug mode.
RegisterServiceCtrlHandlerEx() or RegisterServiceCtrlHandler(). The handler that our service code uses is already decoupled from the main service logic via an internal message queue (implemented with an IOCP) and when in debug mode we simply feed service control messages into the IOCP from a source other than the SCM callback. This 'other source' is simply a thread that listens on a couple of named Win32 event objects. These events can be controlled by a simple gui and allow us to send shutdown and pause/resume controls to the service even when it's running in debug mode.
Cool. I have done something similar but a lot simpler when in debug mode. I use SetConsoleCtrlHandler and translate CTRL+C to shutdown and CTRL+BREAK to suspend/resume depending on state. And used a similar technique in C#. I also don't expose SetStatus to the users of the Service class and Service interface. Instead a spawn off a timer that calls SetStatus with pending shutdown/startup status at regular interval while another thread executes the service startup. This works fairly well and is simple but not as complete as your solution. I also depend on being compiled as console application but that generally makes sense for a service. And seing the service tracing away messages in the console when started in debug gives a cosy, warm feeling that something is happening ;) even when not in the debugger.
Posted by: Michel at November 30, 2005 07:36 PMI like the idea of using CTRL+C etc to control the service... But, I don't build as a console app. I've had a little "two big buttons in a dialog" MFC app that does the shutdown and pause thing for ages as even my non service servers need to be able to do this.
Posted by: Len at November 30, 2005 07:44 PMI have a service that provides ssh for my application, regardless, I need that service to be able to spawn a GUI application. For sake of argument say notepad.exe.
I have tried runas but it does not work. Do you know of a solution?
Thank you so much. HeLp
Posted by: Shawn at January 12, 2006 06:00 PMFor a service to interact with the desktop you need to configure it to do so... Perhaps that's your problem? See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/interactive_services.asp
Posted by: Len at January 12, 2006 07:33 PMThat must be the problem, at least those are the symptoms that match the problem.
Also something interesting - If I ShellEx the application, notepad.exe, from the service with "interact with desktop" selected it runs in a SYSTEM context. If I use CreateProcessWithLogonW to spawn the application from the service with "interact with desktop" selected it will not start the application. Trying to avoid the security problem discussed via your link I figured the service could spawn the application under a more secured context, but NOPE no workin.
Any ideas on that? BTW - Thanks so much for the response and link.
Posted by: Shawn at January 12, 2006 10:12 PMI havent played around with CreateProcessWithLogonW. What error do you get? Access denied or something equally useful? Do you have the user credentials? Can you impersonate and then launch? I'm starting to get out of my depth at this point ;) Good luck.
Posted by: Len at January 12, 2006 10:19 PM