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.
Related
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.
I have an application that checks if a windows service is up and running. This app launches on machine restart and before windows services were instantiated and immediately notifies user saying that the windows service on user machine is not found. but the service starts a bit later.
I am using a C++ MFC application. Is it possible to solve this so that the app will check after all windows services are up and running? and if yes, how
I have a Windows service written in CPP that I start manually. The service starts up just fine and works ok. However we have recently noticed in the Windows Services GUI that the service shows in the Starting state. Upon examining the Event Viewer it clearly shows that the service has entered the Running state.
Anyone got any nuggets of gold on this one?
Thanks.
The problem turned out to be an unaccounted for call to the SCM to start the service. Deleted the line and the problem went away.
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.
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.