How to abort shutdown using c++ winapi???

closed account (3hMz8vqX)
Hi all,
How to abort shutdown using c++ winapi???
I mean I want to detect if windows is shutting down and if so abort it! :)
You're asking how to Abort the System Shutdown?: http://msdn.microsoft.com/en-us/library/windows/desktop/aa376630(v=vs.85).aspx

As for detecting if the system is shutting down, that one is a little more tricky. Maybe hooking InitiateShutdown()?: http://msdn.microsoft.com/en-us/library/windows/desktop/aa376872(v=vs.85).aspx But the correct way to do this is probably to disable shutdown privileges through a GPO. What are you trying to do exactly?
You could always just use system('shutdown /a');
closed account (3hMz8vqX)
@Computergeek01:
I found a way:
Detect shutdown...
GetSystemMetrics(SM_SHUTTINGDOWN)
... Now all I need is a way to abort shutdown...
You can't.

Moreover, you shouldn't. (It's immoral.)


Why, you ask?

Who the heck are you to tell the user he can't shut down his system? (Even if it is your own system.)

Better way is to respond to shutdown signal, save program state and add self to autorun once registry key. When system comes back up, so does your program with state preserved. Calculations may continue, etc.
Get SystemMetrics is a terrible solution since you'll have to constantly run it in a loop. I'll ask again, because I truly believe there is a better way, What is it you are trying to do exactly?

EDIT: I told you how to abort the shutdown. The function is "AbortSystemShutdown()". I may have posted it a little funny but if you had clicked the link I gave you, you would have seen it.
Last edited on
closed account (13bSLyTq)
Entire Code, just the way you like it - I thought you left. Good Luck Copy Pasting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
BOOL PreventSystemShutdown()
{
   HANDLE hToken;              // handle to process token 
   TOKEN_PRIVILEGES tkp;       // pointer to token structure 
 
   // Get the current process token handle  so we can get shutdown 
   // privilege. 
 
   if (!OpenProcessToken(GetCurrentProcess(), 
         TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
      return FALSE; 
 
   // Get the LUID for shutdown privilege. 
 
   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
         &tkp.Privileges[0].Luid); 
 
   tkp.PrivilegeCount = 1;  // one privilege to set    
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 
   // Get shutdown privilege for this process. 
 
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
        (PTOKEN_PRIVILEGES)NULL, 0); 
 
   if (GetLastError() != ERROR_SUCCESS) 
      return FALSE; 
 
   // Prevent the system from shutting down. 
 
   if ( !AbortSystemShutdown(NULL) ) 
      return FALSE; 
 
   // Disable shutdown privilege. 
 
   tkp.Privileges[0].Attributes = 0; 
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
       (PTOKEN_PRIVILEGES) NULL, 0); 
 
   return TRUE;
}
closed account (3hMz8vqX)
@OrionMaster: Thankyou very much!, Sorry I got angry in the last post:(
Orion master amazing .. Thanks
Topic archived. No new replies allowed.