windows service listen for WM_QUERYENDSESSION
windows service listen for WM_QUERYENDSESSION
Any idea "best practice" for a service (or window less app) to listen for #WM_QUERYENDSESSION ?
Re: windows service listen for WM_QUERYENDSESSION
Coded by NM
Modified by RASHAD
Run as invisible window
It will block even Task Manager
Edit : Modified
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
Last edited by RASHAD on Sun Oct 21, 2018 4:37 pm, edited 1 time in total.
Egypt my love
Re: windows service listen for WM_QUERYENDSESSION
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!!
Thanks!!
Re: windows service listen for WM_QUERYENDSESSION
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.
Re: windows service listen for WM_QUERYENDSESSION
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
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
Very helpful, thank you for the links
Re: windows service listen for WM_QUERYENDSESSION
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?