Service Program that programmatically restarts all the crashed Services - c++

C++ windows Service Program which should monitor all services and restart them when they crash.
Any idea how to identify whether the services has crashed or properly stopped ?
which winapi can be used ?

The Service Control Manager can detect a failed (correctly written) service, i.e. the death of a service process without it having returned a SERVICE_STOPPED status or a SERVICE_STOPPED status with a SERVICE_STATUS containing a win32 error code - this is what the Recovery tab does.
You can access this programmatically via the ChangeServiceConfig2() + SERVICE_CONFIG_FAILURE_ACTIONS and then configure it via SERVICE_FAILURE_ACTIONS to say execute an external process.

There is no built-in mechanism in the Windows to determine service termination reason. SCM will report an error if service is unable to start, however if service has been started - any failure reporting is its own business. So if service has been terminated due to the internal errors, killed through the Task Manager, or stopped wia SCM - it will have the same status Service stopped. If services you are trying to monitor has no error/status reporting mechanism, you will be unable to determine its termination reasons.

Related

My dll service disappears after restarting the system

I wrote a dll service in c++ with API functions and working properly. I implemented a ServiceMain() function as dllexport in which call RegisterServiceCtrlHandlerW() function to handle the incoming signals (e.g. stop, pause, ...) of my service.
Every thing working good and I defined all the necessary functions to run the service :
ServiceMain() is defined and RegisterServiceCtrlHandlerW() calls within to register control handler.
Set service status to SERVICE_START_PENDING then to SERVICE_RUNNING to run the service.
I implemented a thread to do stuffs as service jobs and working properly.
And I can start and stop my service.
My service is a dll not exe so I'm using svchost.exe to host it and I did below steps to register in windows registry:
1. I create a new value in registry as a group to introduce my service to svchost.exe as following :
2. I create Parameters subkey And fill a value with my dll (service file) as following :
3. Also I defined a value (ServiceMain) due to introduce my ServiceMain function to svchost.exe as start point.
So far, my service working even in logged off user BUT when I restart my computer, after logon to windows my service disappeared from Windows Service Manager. Actually it is removed from SCM database.
By the way I installed my service with 3 methods. And I'm using svchost.exe as binarypath to install the service (because svchost.exe behave as host for my dll service). For example with sc.exe program I set binarypath to svchost.exe to run my service indirectly with -k groupname as parameters.
My problem is : When I restart my system then my service disappeared (is deleted) after logging in. Please help me to solve this problem.

Application closing before shutdown c++

I have created a console application that runs through a windows service.
Service starts the application and runs it well.
The problem is when i attempt to restart/shutdown my PC, application is closing before the actual shutdown.
So what happens is the service restarts the application again before shutdown.
Even I tried without service. Application is closing before shutdown.
How can I tell my service to know that pc is going to shutdown/restart. So dont restart the application.
Can anyone help me with this ?
Your service can register for notification with RegisterServiceCtrlHandlerEx and will receive the SERVICE_CONTROL_PRESHUTDOWN notification when the shutdown process begins. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms685149(v=vs.85).aspx for more information.

How to capture that session logoff started from windows service

I have a service which constantly checks some application and assures that it wasn't closed. If this app closed - service launches it again.
The problem starts when user decides to log off the session. During logoff all applications are closing including the mine one. But the service is still running and constantly trying to start it again.
The questions is how to notify the service that user is going to log off and the application doesn't need to be restarted anymore? I've tried to make it using SERVICE_CONTROL_SESSIONCHANGE notification. But in accordance to MSDN they come to services when all apps already closed and logging off procedure completed. It is too late for me. Is there any way to programmatically find out that current session is in process of logging off?
My service launched under LocalSystem account.
Thanks.
p.s. I don't have the access to application source code. The goal need to be achieved without modifying it.
Have your service run two applications: the one it is a watchdog for, and a second one which you implement yourself. This second one can then respond to the log-off event by sending a message to the service (a la David Heffernan's answer), and the service will then know not to restart the watched job.
Open a communication channel between your app and your service and arrange for the app to tell the service that it is closing because of a logoff event.
You may not have access to the source code of the executable, but that doesn't mean that you can't affect the process. For instance, you could inject a DLL. Using SetWindowsHookEx, you'd catch the WM_ENDSESSION sent to the apps main window.
Why don't you create your service under the user that is running the App and tell it to startup automatic? In this case you should get the SERVICE_CONTROL_SHUTDOWN message when your user is logging off since the service would also be terminated.

Windows event log service holding executable file handle

I have a service application that on startup and shutdown logs an event log record.
I rebuild the application frequently and also then the executable on the host machine. And here is the problem, after my service shutdown the Windows Eventlog service (not the event log viewer) is holding an open handle to the executable so I cant update it.
I have the event log messages embedded in the executable, i could move it out but then I just move the update problem to another file.
I've double checked and I have paired ::RegisterEventSource/::DeregisterEventSource correctly.
Anyone encountered this problem ?
I've also run into this issue, so just adding some of my experiences.
I have a Windows 2008 Service system (have not seen this on 2003 Server), and when I stop my service, and instance of svchost.exe loads the service executable (visible using vmmap.exe or Process Hacker) preventing it from being deleted/overwritten during uninstall/install. The instance of svchost.exe is running the DHCP Client (Dhcp), TCP/IP NetBIOS Helper (lmhosts), and Windows Event Log (EventLog) services.
In our case, we have created a registry entry to make our service executable an event source. (though I'm unsure exactly why we are doing this, or whether we should be doing this).
Empirically, if I remove that registry entry before stopping the service, the executable is not loaded by svchost.exe and all is fine. If the service has already been stopped and executable loaded by svchost.exe, restarting the Event Log service (or killing the process) also frees up the executable.
I'm guessing our service is not well-behaved (perhaps a side effect of being a 32-bit process on 64-bit OS?) or correctly installed, but haven't isolated the issue yet.
Update: It appears this issue is only happening on HP systems (and not Dell or IBM) which is curious. There are HP-specific management components installed, so perhaps one of them is altering the behavior somehow?
I've also run into this issue. In my case, nxlog service reading logs. Simply stop nxlog service before replace event source file.
I think it is probably the event log viewer. Close the viewer and you'll be fine.

Biztalk web service ports and what happens when the port/application is stopped

I have a question around biztalk and what happens when certain conditions around web service ports are met.
basically we have two applications - a main application (lets call it 'MainApplication') (containing the orchestration) and a web service application (lets call it 'MainApplicationWS'), where we expose a web service (created from biztalks web service tool) to take messages from wherever.
we have a testing tool which replays messages to the MainApplicationWS to simulate messages coming through from various external systems.
I have noticed that if we partial stop the MainApplicationWS application, and send messages through to the web service listed as a recieve location, nothing happens (obviously!) (also, the web service is still running, even though its been delisted as a recieve location). however, if i start up the MainApplicationWS again and bounce the host instances the messages are picked up from somewhere and played through to the orchestration and through to our application.
Im just a bit puzzled as to where its storing these messages while the MainApplicationWS is partially stopped. is the web service somehow hanging on to these? or does it still post through to the biztalk message box?
any clarification would be greatly appreciated :)
cheers,
adam
In short, I can't repeat your behaviour in Biztalk 2009. The closest to 'queueing' messages is if the orchestration is stopped but remains enlisted, such that messages are suspended resumable.
In long - I'm not quite sure what you mean by 'delisted as a receive location'. In Biztalk 2009:
Receive Locations can be enabled or disabled
Orchestrations can be stopped, and unenlisted
A Partial Stop on your BTS application disables receive ports and stops orchestrations (but doesn't unenlist them)
A full stop stops and unenlists orchestrations
The below is observed behaviour on BizTalk 2009 for a simple orchestration with a WCF Request/Response port, which receives a message, Maps the Send back to the same Port
The port is Direct Bound (MessageBox).
If the Isolated Host App Pool is disabled in IIS
A synchronous error is returned to the client - Standard IIS Error (503 Service Unavailable etc)
BizTalk receives no messages at all
If the BizTalk receive Location is disabled
WSDL: Syncrhonous error returned to the client - The Messaging Engine failed to register the adapter for "WCF-BasicHttp" for the receive location "xyz.svc". Please verify that the receive location exists, and that the isolated adapter runs under an account that has access to the BizTalk databases
Service Call : The requested service, xyz.svc could not be activated. See the server's diagnostic trace logs for more information.
If the Orchestration is stopped, but not unenlisted
The received message is Suspended, resumable. The client times out (no response is issued).
If the orch is started and the message resumed, the message is then processed. The client will only get a successful reply if the orch start and the suspended message resume are done before the client's configured WS / WCF timeout.
If the Orchestration is unenlisted
The received message is Suspended, not resumable.
The client receives an error - The server was unable to process the request due to an internal error.
With the WCF CustomBinding it is also possible to listen directly on the relevant BizTalk ReceiveHost (i.e. no need for IIS at all to listen to BasicHTTP or WSHTTP, although we generally still use the Wizard generated svc in IIS solely for the hosting and publication of the WSDL. We then create a new WCF Custom receive location directly in BizTalk and point the client to this)
Hope this helps?