Detecting windows shutdown event - windows-7

I have a windows Service always running once PC started, how would i detect using .NET 3.5 or WMI, when the user or some other application/process has requested a shutdown or reboot in windows.
I do not want to stop it, just log the time and who initiated the shutdown (user or process is fine, don't really need to know which process)
Thank you

Use the Microsoft.Win32.SystemEvents.SessionEnding event.
However, note that you are in a Windows Service, and this event won't fire for you unless you create a hidden form in your service. You can get one of these for free by enabling the "Interact with Desktop" option on the service.

You're looking for the SystemEvents.SessionEnding event.
Occurs when the user is trying to log
off or shut down the system.

Win32.SessionEnded event gets fired when the system is being shut down. You should attach to this in your program.

Related

Does windows stop services gracefully or kills them when an error (memory overflow, etc) occurs?

Firstly sorry for my poor english. I don't really know how to formulate the question, but I can explain you my intentions so it may help you to understand me better.
Im developing tool that notifies you when a windows service goes down.
The exact logic that I follow is:
When a service goes down gracefully logs an event that you can see in windows event viewer. I've created a sheduled task that will be triggered when the service is stopped according to windows event log (Thanks to a XML filter).
This task triggers a powershell script that sends a request to a telegram bot that will notify me when the service dies.
This process works perfectly when I manually stop the service (From service.msc or Powershell's Stop-Service). The objective is to have a realtime track of the service, and in this case works correctly.
The problem comes here: I cannot force the service to crash in order to see if it logs information in windows event viewer.
My questions are:
If an error occurs will windows shut the service down gracefully (like when using Stop-Service) or will it kill the process without registering any log info (like when using taskkill /f)?
Any other suggestions? Is there another way to track a windows service in real time and trigger a script without a loop that runs every certain time.
Hope y'all understand me :)
If a service crashes, you should still see an error message in the event log under Windows Logs > System. The Source will be "Service Control Manager" and Event ID should be either 7031 or 7032 or 7034.
So you can add a filter for these and have your PowerShell script run on these kinds of events as well.

Run background service immediately on registration WindowsPhone 8.1

I have implemented a background service that sends some data to server.
But the service is only triggered after the trigger is fired.
I want the service to fire immediately as and when its registered.
I have tried
DeviceTrigger
LocationTrigger
SystemTrigger(Internet available/Timezone-changed)
MaintenanceTrigger
TimeTrigger
but they all are fired when their conditions are met as documented. Does any one have any idea how to get this work, btw I'm on windows phone 8.1 and C++.
PS. I just want my service to get triggered as it is registered.
AFAIK you can’t do that directly. Microsoft is very strict about what background tasks can and cannot do because battery life depends on it.
There’s a workaround.
Move your server-sending functionality from your background service into a separate DLL. Call that DLL from both background service process, and the GUI process. If you need to, in that DLL you can guard shared resources with a named mutex or use any other IPC mechanism that works across processes.

How to tell a Windows service to restart or reload its configuration?

I have a GUI application that is used to configure a windows service and I want to start, stop or restart this service from thing GUI application.
One of the problems is that this service can run in two modes: windows service or manually from command line (so you have a console).
Now, I am looking for a way to communicate this, a way that would work in both cases.
It may be helpfull to specify that the service process already has and even handler that is dealing with:
CTRL_C_EVENT
CTRL_CLOSE_EVENT
CTRL_BREAK_EVENT
CTRL_LOGOFF_EVENT
CTRL_SHUTDOWN_EVENT
It would be quite easy to extend the handler if I knew what event will be triggered by the service restart option. Still, I should be able to trigger the same event from my GUI application so I will be able to tell the "server" to restart itself even if it is running in background.
The best is would be if I could do this without having to detect in which mode the server application is running.
There are Service Trigger Events that your service can listen for, and then your GUI can send an event. In particular, you want to register for an SERVICE_TRIGGER_TYPE_CUSTOM trigger, which causes your service to listen for an ETW event; your GUI code would then write the ETW event to trigger the event.
There is no service restart action. All you would do is stop the service normally, then start the service normally afterwards. That is all the SCM restart does internally, it is just exposed as a single operation in the SCM UI instead of as two separate options.

intercept shutdown in windows using vbscript

I want to write a vbscript that will wait for windows shutdown and intercept to carry out proper action before shutting down. I want this to be in vbscript since am using it for administration.
Much easier than that, ever since Windows 2000 you can use the Group Policy setting (or Local Policy even if you're not in a domain) to set the Shutdown Script which will be called, as the name implies, when someone is trying to shutdown the computer. For more info, check out MSKB #198642.

preventing vista shutdown loggoff API showing

I want to stop the shutdown / log off API which appears when you have programs open to confirm a shutdown/logoff.
I have an application already that send a reason to the API.
I know this is bad practice but I really need to override / cancel the shutdown with out displaying the API after 5 seconds.
is there any way to hack this or run a custom shutdown script to override the shutdown.
thanks in advance
it doesnt matter about holding it for5 seconds just catching it if it is pressed once. I can do that but vista shows a API screen after 5 seconds which All i want is vista not to show that.
vista shows that because I prevent it from shutting down.
You may want to look at this:
http://msdn.microsoft.com/en-us/library/aa376871(VS.85).aspx
When I did this functionality I had to do everything in unmanaged C++ as I had problems trying to set the priviledge by using Interop.
You need this flag: EWX_FORCE in ExitWindowsEx.

Resources