How do I get #WM_QUIT on reboot?

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 2183
Joined: Sun May 14, 2017 1:48 am

How do I get #WM_QUIT on reboot?

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 6242
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How do I get #WM_QUIT on reboot?

Post by mk-soft »

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 2183
Joined: Sun May 14, 2017 1:48 am

Re: How do I get #WM_QUIT on reboot?

Post by AZJIO »

Thank you, I found examples with these messages. But cmd.exe apparently I will not be able to run.
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How do I get #WM_QUIT on reboot?

Post by jacdelad »

Shouldn't

Code: Select all

Select Event()
  Case #WM_ENDSESSION,#WM_QUERYENDSESSION
  ...
EndSelect
in the main loop work?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2183
Joined: Sun May 14, 2017 1:48 am

Re: How do I get #WM_QUIT on reboot?

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How do I get #WM_QUIT on reboot?

Post by jacdelad »

Can you post the cmd? Maybe you can code it into your pb-program.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: How do I get #WM_QUIT on reboot?

Post 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.
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).
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: How do I get #WM_QUIT on reboot?

Post by ChrisR »

For info only, with GPedit (Windows Pro, enterprise), it is possible to assign computer shutdown scripts.
ZX80
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Dec 12, 2016 1:37 pm

Re: How do I get #WM_QUIT on reboot?

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: How do I get #WM_QUIT on reboot?

Post by Caronte3D »

Maybe you should let the windows to delete the file on the next reboot?
http://forums.purebasic.com/english/vie ... 7f#p337376
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How do I get #WM_QUIT on reboot?

Post 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:
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2183
Joined: Sun May 14, 2017 1:48 am

Re: How do I get #WM_QUIT on reboot?

Post 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.
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: How do I get #WM_QUIT on reboot?

Post 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:
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).
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: How do I get #WM_QUIT on reboot?

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How do I get #WM_QUIT on reboot?

Post 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
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply