detects shutdown and stops it

drkater

New member
Joined
Jun 6, 2019
Messages
3
Programming Experience
1-3
Well, although i already have some coding experience, i've just gotten into C# and i couldn't find any documentation which probably is cause it's relatively unusual. I want to make a programm which, when a checkbox is checked it detects if the pc is going to be shutdown and stops it, so you could call it an "anti-shutdown". Would be perfect if anyone could help me with that, or tell me if C# even is the right language for that.
 
I would first intercept the events responsible for the session currently shutting down and attach an event handler, and then from within that event, I would check if your little checkbox is checked or not, and if it is checked (true), then you can try AbortSystemShutdownA via that call to intercept and then cancel the shutdown process. You can also find more information on the docs regarding the shutdown events. This is also possible to use Pinvoke. - Search Shutdown on the Pinvoke page. If you have a specific question regarding implementing any of these events, post up what you've tried and we can try to help you from there.

Hope this helps.
 
In general, as a Windows Service, you don't want to interfere with the normal starting up and shutting down of Windows. Consider what happens when the OS and patched and needs to be rebooted for the patch to be effective. By preventing the reboot from happening, you may be keeping a critical security fix from being applied to the machine.

Out of curiosity, what is so important about the functionality that your service provides that it needs to be able to prevent Windows from shutting down?
 
In general, as a Windows Service, you don't want to interfere with the normal starting up and shutting down of Windows. Consider what happens when the OS and patched and needs to be rebooted for the patch to be effective. By preventing the reboot from happening, you may be keeping a critical security fix from being applied to the machine.

Out of curiosity, what is so important about the functionality that your service provides that it needs to be able to prevent Windows from shutting down?
Well, i have really slow internet so, downloading updates and so on takes forever and i had it happen several times, that when i left it overnight i'd find my pc shutdown in the morning, so basically my windows shuts itself down completely at some points in time and i want to prevent that, so i dont have to start the whole download fresh from the beginning
 
Is this a Windows Service? They usually don't have GUI and "checkbox".

Also have a read throught this article: Application Shutdown Changes in Windows Vista (Windows) | Microsoft Docs

Thanks for the Article, i'm gonna read it now, but to answer your question, i'll explain it a bit more detailed, i want to create a program, so a simple ui with a checkbox on it, this program should (if the checkbox is checked) detect if windows is going to shut down and if so, stop it from shutting down. Why? for that please read my reply to Skydiver
 
Well, i have really slow internet so, downloading updates and so on takes forever and i had it happen several times, that when i left it overnight i'd find my pc shutdown in the morning, so basically my windows shuts itself down completely at some points in time and i want to prevent that, so i dont have to start the whole download fresh from the beginning
I would first look at the Power Settings you have for configured. Chances are that you have the options to sleep or shutdown after a certain amount of idle time turned on in your current Power Settings. Try changing over to "High Performance". If you've left "High Performance" as its default settings, it won't turn off your computer.

What are you using to download? Most modern web browsers know how to recover from a download that got cut off -- unless you happen to be running in incognito mode. If you are running in incognito mode, then yeah, it is supposed to forget any state you have with any particular web session.

All of the BitTorrent clients that I know of recover from a download that has gotten cut off because that's how the protocol works. It's supposed to reconstruct a file from whatever bits it can get from the Internet. Any bits that are already on your machine don't need to be re-downloaded.
 
i want to create a program, so a simple ui with a checkbox on it
I've moved the thread to Windows Forms forum for now, a Windows Service doesn't seem suitable for this. I also changed the thread title to something descriptive.
 
A Windows Forms form has a FormClosing event and the e.CloseReason property will tell you why it happened, including a Windows shutdown. Cancel closing the form and you prevent Windows shutting down.
 
The e.Cancel = true; will prevent your application from closing, but if someone tries to log off, only your application will prevent windows from logging off. You should note; it won't stop the closure of other applications on the system while the logoff is commencing. When windows shuts down, it collects a list of running applications in need of being closed before the log off can commence. Depending on the order of the opened applications being cued for closure, whatever applications are cued before yours, will also be closed regardless of your applications current state, and windows will only halt when your application is being processed. Any applications cued after your application will also not be shut down. However, a user can still persist to ignore your applications state and force the log off to continue by way of interacting with log off screen. See pic :

session-not-logging-off.jpg


The only way I can think to totally prevent a shutdown would be to perhaps (if possible) change some security policies to prevent a user group from having the ability to log off manually. This isn't something I've tried before, but It might be possible. According to this, there is an option to remove the button from the Start menu. So in theory, you could include using Microsoft.GroupPolicy; and get to work on rewriting the registry values. But yuck, and I would highly recommend you backup your registry in full before playing with a major part of the operating system if you're on a development machine or not. The Microsoft.GroupPolicy will give you much more control over a users ability to log off VIA the UI, but if they know how to reboot and log off via cmd, you may have a new problem on your hands to prevent that. Some here might argue that group policy might not be the best way to go about it.

~Added missing link
 
Last edited:

Latest posts

Back
Top Bottom