Recipe 10.8. Running a Task Automatically via the Registry


Problem

You want to run a task when a user logs on to a system.

Solution

There are four avenues you can use to make tasks run automatically after a user logs on: the Registry, startup folders, login scripts, and Group Policy. We explain how to use login scripts in Recipe 10.9 and Group Policy in Recipe 10.10. Here we'll describe the Registry and startup folder options.

Registry

There are four Registry keys that you can use to run tasks automatically. To use any of the keys, simply create a value entry of type REG_SZ under the key. Give the value any name you want and specify the full path to the program or script and any parameters as the value data. See Figure 10-1 for some example entries.

Figure 10-1. Sample Registry Run key values


Values defined under the following key cause tasks to run for every user that logs into the system:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

Values defined under the following key cause tasks to run whenever the user who is currently logged on logs on to the system:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Values defined under the following key cause a task to run the next time any user logs into the system (and not after that):

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce

Values defined under the following key cause a task to run the next time the current user logs into the system:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

The values under the RunOnce keys are deleted once the tasks start.


Startup Folders

Similar to the Run and RunOnce Registry keys, programs contained in the startup folders run after a user logs in. Generally, shortcut files (.lnk files), not the actual programs, are placed in the startup folders, but you can do it either way.

Programs placed in the following folder are run after any user logs into the system:

%ALLUSERSPROFILE%\Start Menu\Programs\Startup

Programs placed in this folder are run for a specific user:

%USERPROFILE%\Start Menu\Programs\Startup

Discussion

By default, tasks defined under the Run and RunOnce keys are not executed if the system is booted into Safe mode. You can force tasks defined under the RunOnce keys to execute even if the system is booted into Safe mode by prefixing the value names under the RunOnce key with an asterisk (*).

RunOnce value entries may also be prefixed with an exclamation point (!), which causes the associated task not to be deleted until after the program completes. Without this, each value entry is deleted before each task is executed. If there is a failure in the task, it may result in the task not completing successfully.

Multiple value entries under the Run or RunOnce keys and multiple programs in the Startup folders are loaded in an indeterminate order, so you can't assume one will run before another. If this is an issue for you, you could create a batch file that calls the programs in a particular order and put only the batch file in the Run or RunOnce key.

There are several other Registry keys that can cause tasks to run automatically. For a complete list of all of these Registry keys, see Recipe 10.11.

See Also

Recipe 10.11 for more on viewing all automatic tasks; MS KB 137367, "Definition of the RunOnce Keys in the Registry," MS KB 179365, "INFO: Run, RunOnce, RunServices, RunServicesOnce and Startup," and MB KB 314866, "A Definition of the Run Keys in the Windows XP Registry"



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net