Exit Signal Handler?

Started by User_McUser, March 18, 2025, 11:01:11 AM

Previous topic - Next topic

User_McUser

Is there a way to set a handler for system exit requests?

For example, if Windows is shutting down, I want to catch that and save some data to the registry without stopping the shutdown. I set IntControl(12,5,"",0,0) already but I don't see where I can put code to respond to a shutdown event.

On a semi-related note, my app does not close with Alt+F4 - what do I need to add to allow that key-combo to exit my app? 🤔

Thanks! 🙂

td

Numerous approaches are available on the Tech Database website (link at the top of this forum). Use the "Windows shutdown" search term.

Alt+f4 generally terminates an application if it is the foreground window when the key combo is pressed. So you need to make your app the foreground window.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

1) techsupt.winbatch.com is 404 (that's what I get when I click the button you referred to)

2) Winbatch scripts generally don't respond to Alt/F4.  Generally, you have to do Ctrl/Break (or click Terminate from the Alt/space menu).  It seems to me that WB has always gone out of its way to make itself (i.e., a running script) hard to kill.  I assume there are (no doubt, good) policy reasons behind this.

OP should check out IntControl(49).  I remember using that long ago, but have no recent experience with it (and am not in a position to test it at the moment).

User_McUser

Quote from: td on March 18, 2025, 01:35:46 PMNumerous approaches are available on the Tech Database website (link at the top of this forum). Use the "Windows shutdown" search term.

Alt+f4 generally terminates an application if it is the foreground window when the key combo is pressed. So you need to make your app the foreground window.

Alt+F4 definitely does not close my script - in fact when I do that, it activates one of the buttons on the dialog, which is weird. 🤷

Searching the database, it looks like IntControl(1005) *used* to do what I want but was depreciated (Or I think it would have - the documentation is unclear on the function other than indicating it is no longer supported and was scrubbed from documentation due to problems with NT and Win2000, which kinda gives away the age of the document.) The proposed solution is to create and launch a services that monitors for the shutdown event but that isn't going to work for me. The solution I am now considering is to add a timer to the dialog callback and just check if the values I want to store at exit have changed since the last time I wrote them out to disk and if so, write them out.

Thanks @snowsnowsnow - I'll check that out. 👍


JTaylor

Not sure I can help but can you explain the situation a little more?   Is this a case where you have a Dialog/Script that is running and if the system gets shutdown you want it to do something or are you wanting a script whose purpose is to monitor the system and watch for a shutdown and take the desired actions?

Jim

spl

Quote from: JTaylor on March 19, 2025, 02:01:32 PMNot sure I can help but can you explain the situation a little more? 
Jim

I agree with Jim.
  • is the shutdown deliberate or random
  • if random, does it normally happen running a WB script
  • if deliberate, why??

I remember a lot of blue screens of death with XP/Win7, a few with Win10, but none so far with Win11.
Stan - formerly stanl [ex-Pundit]

snowsnowsnow

I've never had any BSOD on either Win7, Win10, or Win11.

td

Quote from: snowsnowsnow on March 19, 2025, 09:20:20 AM1) techsupt.winbatch.com is 404 (that's what I get when I click the button you referred to)

2) Winbatch scripts generally don't respond to Alt/F4.  Generally, you have to do Ctrl/Break (or click Terminate from the Alt/space menu).  It seems to me that WB has always gone out of its way to make itself (i.e., a running script) hard to kill.  I assume there are (no doubt, good) policy reasons behind this.

OP should check out IntControl(49).  I remember using that long ago, but have no recent experience with it (and am not in a position to test it at the moment).


1. It has been working just fine and there have been no other reports of issues. I do not know why you have/had the problem.
2. The OP said "my app". He did not say a WinBatch script. You can use IntControl 1008 to enable Alt+f4. You may also need to perform error handling to avoid an error message box.

The reason IntControl 1005 is deprecated is explained here.
https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Functions+IntControl~1005.txt

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Quote from: User_McUser on March 19, 2025, 01:15:25 PMAlt+F4 definitely does not close my script - in fact when I do that, it activates one of the buttons on the dialog, which is weird. 🤷

Searching the database, it looks like IntControl(1005) *used* to do what I want but was depreciated (Or I think it would have - the documentation is unclear on the function other than indicating it is no longer supported and was scrubbed from documentation due to problems with NT and Win2000, which kinda gives away the age of the document.) The proposed solution is to create and launch a services that monitors for the shutdown event but that isn't going to work for me. The solution I am now considering is to add a timer to the dialog callback and just check if the values I want to store at exit have changed since the last time I wrote them out to disk and if so, write them out.


The Tech Database also mentions using group policy registry settings. You can use gpedit.msc to see how the registry is changed. You could duplicate it via script if you need that. A Tech Database article also mentions where to find GP changes.

BTW, NT and Win2000 are still at the core of the Windows OS.  That kind of gives away the age of the 32-bit and 65-bit Windows OS. 

Check out IntControl 1008 in the on-line and help file documentation. It allows scripts to be terminated by the alt+f4 system menu shortcut key combination.

[edit] The gpedit approach is explained here; https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/How~To+Specify~a~Script~to~Run~on~Startup~Shutdown~Logon~Logoff.txt

Yes it mentions NT Windows XP but that means very little. The more things change the more they stay the same. This is particularly true of the Windows OS.
[/edit]
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Use the following IntControl calls to terminate a script silently when it has the focus and the alt+f4 key combination is pressed.

IntControl(1008,1,0,0,0)
IntControl(12,4,0,0,0)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

Quote1. It has been working just fine and there have been no other reports of issues. I do not know why you have/had the problem.

It works in some test environments and not in others.  I think it is related to CloudFlare - which a lot of sites are using these days and which tends to really mess things up.

Are you using CloudFlare?

td

We use CloudFlare for CDN services.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Since we have already wandered far off-topic, here is what Cloudflare says about the problem:
"Cloudflare does not generate 404s for customer websites, we only proxy the request from the origin server. If you encounter a 404 error on a Cloudflare-powered site, the issue lies with the origin server. In such cases, contact your hosting provider for assistance."

Me thinks this is a case of "blameless washing." While they may not generate the error, that does not mean they don't cause it.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

User_McUser

QuoteThe Tech Database also mentions using group policy registry settings. You can use gpedit.msc to see how the registry is changed. You could duplicate it via script if you need that. A Tech Database article also mentions where to find GP changes.

BTW, NT and Win2000 are still at the core of the Windows OS.  That kind of gives away the age of the 32-bit and 65-bit Windows OS. 

I believe there has been a misunderstanding; I don't need to add a startup/shutdown script to Windows. What I need is for my WinBatch app to recognize when Windows is restarting or shutting down so it can write certain information out to disk and then exit gracefully instead of just being killed.

When a shutdown or restart is initiated, Windows typically sends a message to all running processes letting them know so they can clean up and gracefully exit; I want to receive and then act on that message accordingly.

I am also well aware of the history of the NT kernel and that all modern Windows systems are derivations there-of; I've been in the industry since the late 90s. 👍

QuoteCheck out IntControl 1008 in the on-line and help file documentation. It allows scripts to be terminated by the alt+f4 system menu shortcut key combination.

This is very helpful, thank you. 🙂

td

Quote from: User_McUser on March 21, 2025, 10:22:53 AMBTW, NT and Win2000 are still at the core of the Windows OS.  That kind of gives away the age of the 32-bit and 65-bit Windows OS. 

Kind of a youngin. You miss out on the punch cards.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

User_McUser

Quote from: td on March 21, 2025, 10:40:52 AM
Quote from: User_McUser on March 21, 2025, 10:22:53 AMBTW, NT and Win2000 are still at the core of the Windows OS.  That kind of gives away the age of the 32-bit and 65-bit Windows OS. 

Kind of a youngin. You miss out on the punch cards.

Just make sure they're all 9-side down...

td

InteControl 12 works, in part, by intercepting one of the signals you referred to. So you could use IntControl 12 to create a form of pseudo-critical section: 

IntControl (12, 2, 0, 0, 0)
; Do something critical.
IntControl (12, 1, 0, 0, 0)

It is far from a perfect solution with several drawbacks.

Also, do not fold, spindle, or mutilate.

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade