Page 1 of 2

How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 8:00 am
by AZJIO
I want to delete several files when shutting down Windows10. How do I get #WM_QUIT events to terminate my program correctly?

Code: Select all

Procedure WinCallback(hWnd, uMsg, wParam, lParam)
	Protected Result = #PB_ProcessPureBasicEvents
	Protected msg.MSG
	PeekMessage_(@msg.MSG, #Null, #WM_QUIT, #WM_QUIT, #PM_REMOVE)
	If  msg\message & #WM_QUIT 
		RunProgram(ExeDir$ + "Delete.cmd", "2", ExeDir$, #PB_Program_Hide)
	EndIf
	Select uMsg

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 10:05 am
by mk-soft

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 10:40 am
by AZJIO
Thank you, I found examples with these messages. But cmd.exe apparently I will not be able to run.

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 10:58 am
by jacdelad
Shouldn't

Code: Select all

Select Event()
  Case #WM_ENDSESSION,#WM_QUERYENDSESSION
  ...
EndSelect
in the main loop work?

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 11:09 am
by AZJIO
The message works, and the launch of cmd.exe does not work. If the system received the completion code, it does not allow you to run applications.

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 1:51 pm
by jacdelad
Can you post the cmd? Maybe you can code it into your pb-program.

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 3:50 pm
by Axolotl
I am not sure if this is a good idea. If the deletion takes longer, there are timeouts which can strike.
I have never used the commands to prevent the shutdown. (MSDN)

Maybe it is easier to postpone the deletion until the next Windows startup?

Example : delete file at next Windows startup

Code: Select all

MoveFileEx_("Some_file_i_dont_need_anymore", 0, #MOVEFILE_DELAY_UNTIL_REBOOT)  
MSDN: If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 4:07 pm
by ChrisR
For info only, with GPedit (Windows Pro, enterprise), it is possible to assign computer shutdown scripts.

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 6:24 pm
by ZX80
@AZJIO

I think so:

You say that after the operating system generates a shutdown signal, you are no longer able to run any program. Okay. Then what prevents you for doing the necessary actions/stuff in the program that caught this signal? It's already working, isn't it? Why do you need cmd? Why deferred deletion? I really do not understand.

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 8:11 pm
by Caronte3D
Maybe you should let the windows to delete the file on the next reboot?
http://forums.purebasic.com/english/vie ... 7f#p337376

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 8:26 pm
by jacdelad
It's funny how everyone assumes there's a file or directory to be deleted and actually we don't know what's really done in "delete.cmd", yet. :lol:

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 9:33 pm
by AZJIO
I need to talk to the author of the *.cmd file to discuss the problem. The program should not limit the author of *.cmd in the possibilities, but in an exceptional case it will have to be done. The solution may be an ini file.

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 10:06 pm
by Axolotl
jacdelad wrote: Tue Oct 05, 2021 8:26 pm It's funny how everyone assumes there's a file or directory to be deleted and actually we don't know what's really done in "delete.cmd", yet. :lol:
I read the first sentence in the first post. :wink:

Re: How do I get #WM_QUIT on reboot?

Posted: Tue Oct 05, 2021 11:27 pm
by ChrisR
I found this in Shutdown Changes for Windows Vista

Code: Select all

Note that the system does not allow console applications or applications without a visible window to cancel shutdown.

Try with this code, based on celtic88's code, to Prevent System Shutdown

Code: Select all

Prototype.b ShutdownBlockReasonCreate(hwnd.i,pwszReason.p-unicode)
Global ShutdownBlockReasonCreate_.ShutdownBlockReasonCreate
Prototype.l ShutdownBlockReasonQuery(hwnd.i,*pwszBuff,*pcchBuff)
Global ShutdownBlockReasonQuery_.ShutdownBlockReasonQuery
Prototype.b ShutdownBlockReasonDestroy(hwnd.i)
Global ShutdownBlockReasonDestroy_.ShutdownBlockReasonDestroy
Global Quit.b

If OpenLibrary(0,"user32.dll") 
  ShutdownBlockReasonCreate_ = GetFunction(0,"ShutdownBlockReasonCreate") 
  ShutdownBlockReasonQuery_ = GetFunction(0,"ShutdownBlockReasonQuery")
  ShutdownBlockReasonDestroy_ = GetFunction(0,"ShutdownBlockReasonDestroy")
  CloseLibrary(0)
EndIf

Procedure WindowsCallback(hWnd, uMsg, wParam, lParam)
  If uMsg=#WM_QUERYENDSESSION
    Protected BufSize = 0
    *BufSize = @BufSize
    If ShutdownBlockReasonQuery_(hWnd, 0, *BufSize)

      ;===== Process to do before system Shutdown  =====
      ; https://docs.microsoft.com/en-us/windows/win32/shutdown/shutdown-changes-For-windows-vista
      ; Note that the system does not allow console applications or applications without a visible window to cancel shutdown
      
      ; Uncomment the next line to frees the reason string and indicates that the system can be shut down 
      ;ShutdownBlockReasonDestroy_(hWnd)
        
      Quit = #True
      ProcedureReturn 0
    EndIf
  Else
    ProcedureReturn #PB_ProcessPureBasicEvents
  EndIf
EndProcedure

OpenWindow(0,0,0,220,120, "Prevent Shutdown", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowCallback(@WindowsCallback())

SetProcessShutdownParameters_($03FF, 0)
ShutdownBlockReasonCreate_(WindowID(0), "Application blocked before system Shutdown to do...")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = #True
  EndSelect
Until Quit = #True

Re: How do I get #WM_QUIT on reboot?

Posted: Wed Oct 06, 2021 12:14 am
by jacdelad
Axolotl wrote: Tue Oct 05, 2021 10:06 pm
jacdelad wrote: Tue Oct 05, 2021 8:26 pm It's funny how everyone assumes there's a file or directory to be deleted and actually we don't know what's really done in "delete.cmd", yet. :lol:
I read the first sentence in the first post. :wink:
Ah, you're right. But knowing the exact content would still help