windows service listen for WM_QUERYENDSESSION

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

windows service listen for WM_QUERYENDSESSION

Post by jassing »

Any idea "best practice" for a service (or window less app) to listen for #WM_QUERYENDSESSION ?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4622
Joined: Sun Apr 12, 2009 6:27 am

Re: windows service listen for WM_QUERYENDSESSION

Post by RASHAD »

Coded by NM
Modified by RASHAD
Run as invisible window
It will block even Task Manager

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 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
Edit : Modified
Last edited by RASHAD on Sun Oct 21, 2018 4:37 pm, edited 1 time in total.
Egypt my love
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: windows service listen for WM_QUERYENDSESSION

Post by jassing »

Thank you Rashad -- I was under the impression that a windows service could have no UI elements. I guess I should have tried that anyway.

Thanks!!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: windows service listen for WM_QUERYENDSESSION

Post by Dude »

Rashad, for Windows 7 at least, you can't block Windows shutdown like that anymore, even with an invisible window. I've had this problem for years now and was hoping your code was the solution I was waiting for, but it's not. My PC shut down three times testing it. :( And Task Manager wasn't blocked, either. A shame, because the code looked so promising. 8)
fryquez
Enthusiast
Enthusiast
Posts: 360
Joined: Mon Dec 21, 2015 8:12 pm

Re: windows service listen for WM_QUERYENDSESSION

Post by fryquez »

It's actually described, how to do it in Service Control Handler Function by MS.

Also an interesting info about the different handling of shutdown in CUI, GUI and service: Handling the OS shutdown event using WinAPI
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: windows service listen for WM_QUERYENDSESSION

Post by jassing »

Very helpful, thank you for the links
Starmary
New User
New User
Posts: 2
Joined: Fri Apr 09, 2021 8:32 am
Contact:

Re: windows service listen for WM_QUERYENDSESSION

Post by Starmary »

Why do other apps still shut down even when I return FALSE in the WM_QUERYENDSESSION message? Why does this happening and what can be done to make my app prevent other apps from closing as well?
Post Reply