Page 1 of 1

windows service listen for WM_QUERYENDSESSION

Posted: Sun Oct 21, 2018 4:10 am
by jassing
Any idea "best practice" for a service (or window less app) to listen for #WM_QUERYENDSESSION ?

Re: windows service listen for WM_QUERYENDSESSION

Posted: Sun Oct 21, 2018 1:25 pm
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

Re: windows service listen for WM_QUERYENDSESSION

Posted: Sun Oct 21, 2018 1:49 pm
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!!

Re: windows service listen for WM_QUERYENDSESSION

Posted: Sun Oct 21, 2018 1:59 pm
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)

Re: windows service listen for WM_QUERYENDSESSION

Posted: Sun Oct 21, 2018 4:37 pm
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

Re: windows service listen for WM_QUERYENDSESSION

Posted: Sun Oct 21, 2018 5:15 pm
by jassing
Very helpful, thank you for the links

Re: windows service listen for WM_QUERYENDSESSION

Posted: Wed May 19, 2021 8:40 am
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?