Cancel shutdown/reboot?

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Cancel shutdown/reboot?

Post by jassing »

I was using code like the below, however, it doesn't seem to be working. Seems to slow it down; but doesn't cancel it.
I think I read that this ability was removed some time ago...
Is it still possible ?

Code: Select all

Prototype ProtoShutdownBlockReasonCreate(hWnd.i, *pwszReason)
Prototype ProtoShutdownBlockReasonDestroy(hWnd.i)

If OpenLibrary(0, "user32.dll")
  Global ShutdownBlockReasonCreate.ProtoShutdownBlockReasonCreate = GetFunction(0, "ShutdownBlockReasonCreate")
  Global ShutdownBlockReasonDestroy.ProtoShutdownBlockReasonDestroy = GetFunction(0, "ShutdownBlockReasonDestroy")
  CloseLibrary(0)
EndIf

Procedure WindowsCallback(hWnd,uMsg,wParam,lParam)
  Result = #PB_ProcessPureBasicEvents
  Select uMsg
    Case  #WM_QUERYENDSESSION   
      why.s = "Some file still in progress."   
      ShutdownBlockReasonCreate(WindowID(0), @why.s)
      ;result = MessageRequester("Warning", why.s+" Abort shutdown?", #PB_MessageRequester_YesNoCancel)
      If #True ;result = #PB_MessageRequester_Yes
        AbortSystemShutdown_(#Null)
        ProcedureReturn #False
      Else
        ShutdownBlockReasonDestroy(WindowID(0))
        ProcedureReturn #True
      EndIf      
    
    Case #WM_NCACTIVATE
      ProcedureReturn #True
      
  EndSelect      
  ProcedureReturn Result
EndProcedure

If OpenWindow(0,0,0,200,100,"Shutdown and wait")
SetWindowCallback(@WindowsCallback())
Repeat
 
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Cancel shutdown/reboot?

Post by BarryG »

Maybe this helps -> https://stackoverflow.com/questions/753 ... om-message

It says: "If you need to block shutdown you call ShutdownBlockReasonCreate passing the handle to your main window and the reason as a string. This string is what is displayed in the shutdown blocked dialog, i.e. "1 virtual machine is in use" in your screenshot."
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Cancel shutdown/reboot?

Post by jassing »

Thanks; but that's basically what's happening...
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Cancel shutdown/reboot?

Post by Axolotl »

As far as I understand the entire process, you have to keep in mind that:

a) The AbortSystemShutdown function stops a system shutdown started by using the InitiateSystemShutdown function.
b) To stop the local computer from shutting down, the calling process must have the SE_SHUTDOWN_NAME privilege.

Code: Select all

; Example Code (translated from some C code) 
; not tested, only as a source of ideas  
; 
#SE_SHUTDOWN_NAME = "SeShutdownPrivilege" 

Procedure PreventSystemShutdown() 
  Protected hToken                ; // handle to process token 
  Protected tkp.TOKEN_PRIVILEGES  ; // pointer to token structure 

; // Get the current process token handle  so we can get shutdown privilege. 
  If Not OpenProcessToken_(GetCurrentProcess_(), #TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY, @hToken) 
    ProcedureReturn #False 
  EndIf 
  
; // 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, #Null, 0) 

  If GetLastError_() <> #ERROR_SUCCESS  
    ProcedureReturn #False  
  EndIf 

; // Prevent the system from shutting down. 
  If Not AbortSystemShutdown_(#Null) 
    ProcedureReturn #False  
  EndIf 

; // Disable shutdown privilege. 
  tkp\Privileges[0]\Attributes = 0 
  AdjustTokenPrivileges_(hToken, #False, @tkp, 0, #Null, 0) 

  ProcedureReturn #True 
EndProcedure 
For the use of the other functions, MSDN wrote:
Applications should call this function as they begin an operation that cannot be interrupted, such as burning a CD or DVD. When the operation has completed, call the ShutdownBlockReasonDestroy function to indicate that the system can be shut down.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Cancel shutdown/reboot?

Post by jassing »

thanks. Maybe this is my problem -- the process should hold the shutdown indefinitely...
I'll have a view of your code vs what I'm doing to see what's different.

thanks
-j
Post Reply