WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Reboot and Shutdown Topics

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Watch for Shutdown and Perform a Series of Things: A detailed discussion

Keywords: 	 shutdown intcontrol(1005

NOTICE:

IntControl 1005 was removed from documentation as it did not work as people wished. It worked on on Windows 95/98/ME machines, but was not at all satifactory on NT/2000/XP/2003 machines.

WORKAROUND:

This is a possible workaround for IntControl 1005 on NT platforms. The idea is to Launch a WinBatch service (.exs) and have it monitor for the Shutdown/Logoff message, execute any shutdown/logoff code then exit! You can install the service on the fly using wntSvcCreate then start it using wntSvcStart. http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Functions+IntControl~1005.txt


Question:

I want to launch a unattended setup when users log off. I use the IntControl function to trigger when the user is trying to log off or reboot the machine. But when the users log of I get a message stating the program cannot start because windows is shutting down... I have used the following script:
rc = IntControl(1005, 0, 0, 0, 0) ; get shutdown state
Message("ShutDown State=", rc)

If rc == @FALSE ; if we're running as a normal script
new=IntControl(1005, 1, 0, 0, 0) ; enter shutdown mode
Message("ShutDown State=", new)
Exit ; this 'Exit' is not really necessary
Endif

; if we got here, we're running in shutdown mode
Display(1, "Windows Shutdown", "WinBatch shutdown processing begins now")
ErrorMode(@OFF)
RUNWAIT("c:\winnt\options\setup.exe","c:\winnt\options\u.txt)
ErrorMode(@CANCEL)
Does anybody have a clue how I can trygger a shutdown and cancel it, until I give the function to reboot ?

Answer:

Scenario:

You have WinNT workstations on your network and they are logged into some network servers, either a NT domain or to Netware servers (e.g. NDS). Some time during the login process you had a script run on the workstation, either copied locally and then run or else just run from a network drive letter. This script uses IntControl(1005...) to go into shutdown mode. When the user goes to logout you want the script to wake up and run the setup program for a service pack. When the script gets woken up in shutdown mode then it tries to launch the service pack setup program. However, this fails because the computer is already shutting down.

If I have a correct understanding of this situation then here are some ways to help you deal with this. I also have a few questions for you.

Q: Do your users have full administrator rights on these workstations or are they limited in some way? This is important to know because it limits what types of changes can be made (registry, files) while running in the context of any particular user's account.

Warning: Be aware that IntControl(1005...) is really only intended to allow a script to perform a very short sequence of events before terminating. A script which has called IntControl(1005) and is waiting for a shutdown to occur has already committed itself to answering "YES" when Windows asks the script if it is OK to shutdown. This is what allows the script to actually wake back up again when the end session message is sent out to all running applications. If your script was set to ignore this end session message (run as a service) then it would never wake back up again when the user starts the shutdown process. Also, please note that your script has about 20 seconds in which to complete its "shutdown" processing before it must terminate or else Windows will put up a dialog box indicating that your script is not responding to the shutdown request and would you like to end that task forcibly or wait for it to complete on its own. I don't like giving the user the chance to kill one of my scripts if it is important that the script run to completion.

Another thing to be aware of is that WinNT and Win9x both handle shutdown a little bit differently, so a script that works correctly on one of those platforms won't necessarily work properly on the other.

WinNT starts the shutdown process by asking each program if it is OK to exit from windows. As each program answers YES Windows tells it to close itself. The result is that if Windows progresses through the list of running programs and encounters one that refuses to shutdown then it aborts the shutdown but there could be a whole lot of applications that were already successfully closed. The order in which applications are queried for approval to shutdown is based on the order in which the applications were launched. If one application launched very early on say NO to the shutdown query question then none of the applications launched after it will every be queried or told to shutdown. Any application that says YES to the shutdown query but fails to respond to the shutdown command within 20 seconds will be subject to the "wait / end task" processing.

Win9x starts the shutdown process by asking each program if it is OK to exit from windows. Win9x gets the results from all applications before determining if it is OK to shutdown. If at least one application refuses to OK the shutdown request then the shutdown process is aborted and no applications are told to close. If all applications answer YES to the shutdown query then Win9x will issue a shutdown command to each application. Any application that fails to respond to the shutdown command within 20 seconds will be subject to the "wait / end task" processing.

So, with one question asked and one major warning delivered about shutdown processing, here is my recommendation. This approach assumes that the user is fully privileged and can install the service pack if the service pack.

  1. Write a control script that interacts with the user to notify them of a need to close all applications. This script will also run the service pack installation.

  2. Write a signal script that interacts with the control script to signal notification of a shutdown event.
The control script should use IntControl(12,10,"",0,0) to set itself up to never be terminated by CTRL+Break and to NOT allow Windows to exit.

The signal script should use IntControl(12,9,"",,0,0) to set itself up to never be terminated by CTRL+Break and to allow Windows to exit.

Both scripts should run hidden so that they don't leave any buttons on the task bar.

IMPORTANT: The signal script must be launched before the control script. Ideally, the signal script would launch the control script. If the signal script gets launched after the control script then it won't get killed by the user's attempt to shutdown the system and thus the control script will simply abort the shutdown process w/o ever knowing that the user attempted to shutdown or logout from the computer.

The signal script's purpose is simply to exist with a hidden window with a window title that the control script can detect. The signal script can use a long "TimeDelay(3600)" in an infinite "While @TRUE" loop so that it takes very little CPU resources to run signal script.

The control script will start out checking to see if the signal script's window exists. If not, then abort with an error. If yes then use a WinWaitClose() function call to wait for the signal script to have terminated. Once the signal script has terminated we can assume that the user tried to do a shutdown, that the signal script was queried, answered YES and was told to terminate and that the control script was queried, answered NO and caused the shutdown to abort. Now the control script comes to life and takes action.

At this point, the control script should make its window visible and put up a box window or a message dialog indicating that the user should close all applications and then click the appropriate button on the message displayed by the control script to start the service pack installation. Once the user clicks the proper button the control script should run the service pack installation and wait for it to complete. After the service pack installation is complete then the control script can allow Windows to exit by calling IntControl(12,9,"",0,0) and finally it should reboot the computer via IntControl(67,0,0,0,0).


Article ID:   W14484
Filename:   Watch for Shutdown and Perform a Series of Things - 2.txt
File Created: 2014:07:18:09:51:38
Last Updated: 2014:07:18:09:51:38