MSDN -> Creating an AutoRun-Enabled Application -> Autorun.inf Commands -> Enabling and Disabling AutoRun -------------------------------------------------------------------------------- (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/autorun/autoplay_works.asp) ----- Creating an AutoRun-Enabled Application -------------------------------------------------------------------------------- Creating an AutoRun-enabled application is a straightforward procedure. This topic uses CD-ROM as an example (it was the first medium to implement this technology) but today there are many different media types that can use it. To enable AutoRun in your application, you simply include two essential files: An Autorun.inf file A startup application When a user inserts a disc into a CD-ROM drive on a AutoRun-compatible computer, the system immediately checks to see if the disc has a personal computer file system. If it does, the system searches for a file named Autorun.inf. This file specifies a setup application that will be run, along with a variety of optional settings. The startup application typically installs, uninstalls, configures, and perhaps runs the application. Creating an Autorun.inf File The [DeviceInstall] Section Tips for Writing AutoRun Startup Applications Creating an Autorun.inf File Autorun.inf is a text file located in the root directory of the CD-ROM that contains your application. Its primary function is to provide the system with the name and location of the application's startup program that will be run when the disc is inserted. Note Autorun.inf files are not supported under Microsoft® Windows® XP for drives that return DRIVE_REMOVABLE from GetDriveType. The Autorun.inf file can also contain optional information including: The name of a file that contains an icon that will represent your application's CD-ROM drive. This icon will be displayed by Windows Explorer in place of the standard drive icon. Additional commands for the shortcut menu that is displayed when the user right-clicks the CD-ROM icon. You can also specify the default command that is run when the user double-clicks the icon. Autorun.inf files are similar to .ini files. They consist of one or more sections, each headed by a name enclosed in square brackets. Each section contains a series of commands that will be run by the Shell when the disc is inserted. There are two sections that are currently defined for Autorun.inf files. The [autorun] section contains the default AutoRun commands. All Autorun.inf files must have an [autorun] section. An optional [autorun.alpha] section can be included for Microsoft Windows NT® 4.0 systems running on RISC-based computers. When a disc is inserted in a CD-ROM drive on a RISC-based system, the Shell will run the commands in this section instead of those in the [autorun] section. Note The Shell checks for an architecture-specific section first. If it does not find one, it uses the information in the [autorun] section. After the Shell finds a section, it ignores all others, so each section must be self-contained. Each section contains a series of commands that determine how the Autorun operation takes place. There are five commands available. Command Description defaulticon Specifies the default icon for the application. icon Specifies the path and file name of an application-specific icon for the CD-ROM drive. open Specifies the path and file name of the startup application. shell Defines the default command in the CD-ROM's shortcut menu. shell\verb Adds commands to the CD-ROM's shortcut menu. The following is an example of a simple Autorun.inf file. It specifies Filename.exe as the startup application. The second icon in Filename.exe will represent the CD-ROM drive instead of the standard drive icon. [autorun] open=Filename.exe icon=Filename.exe,1 This Autorun.inf sample runs different startup applications depending on the type of computer. [autorun] open=Filename_x86.exe icon=IconFile.ico [autorun.alpha] open=Filename_RISC.exe icon=IconFile.ico The [DeviceInstall] Section You can use the [DeviceInstall] section on any removable media. It is supported only under Windows XP. You use its one command, DriverPath, to specify a directory path where Windows XP searches for driver files, which prevents a lengthy search through the entire contents. You use the [DeviceInstall] section with a driver installation to specify directories where Windows XP should search the media for driver files. Under Windows XP, entire media are no longer searched by default, therefore requiring [DeviceInstall] to specify search locations. The following are the only removable media that Windows XP fully searches without a [DeviceInstall] section in an Autorun.inf file. Floppy disks found in drives A or B. CD/DVD media less that 1 gigabyte (GB) in size. All other media must include a [DeviceInstall] section for Windows XP to detect any drivers stored on that media. Note As with the [AutoRun] section, the [DeviceInstall] section can be architecture-specific. Tips for Writing AutoRun Startup Applications There are essentially no constraints on how to write an AutoRun startup application. You can implement it to do whatever you find necessary to install, uninstall, configure, or run your application. However, the following tips provide some guidelines to implementing an effective AutoRun startup application. Users should receive feedback as soon as possible after they insert an AutoRun compact disc into the CD-ROM drive. Startup applications should thus be small programs that load quickly. They should clearly identify the application and provide an easy way to cancel the operation. Typically, the initial part of the startup application presents users with a user interface, such as a dialog box, asking them how they would like to proceed. Check to see if the program is already installed. If not, the next step will probably be the setup procedure. The startup application can take advantage of the time the user spends reading the dialog box by starting another thread to begin loading the setup code. By the time the user clicks OK, your setup program will already be partly if not fully loaded. This approach significantly reduces the user's perception of the amount of time it takes to load your application. If the application has already been installed, the user probably inserted the disk to run the application. As with the setup case, you can start another thread to begin loading application code to shorten the waiting time for the user. Hard disk space may be a limited resource on many systems. Here are a few hints for minimizing hard disk usage: Keep the number of files that must be on the hard disk to a minimum. They should be restricted to files that are essential to running the program or that would take an unacceptably large amount of time to read from the CD-ROM. In many cases, installing nonessential files on the hard disk is not necessary, but may provide benefits such as increased performance. Give the user the option of deciding how to make the tradeoff between the costs and benefits of hard disk storage. Provide a way to uninstall any components that were placed on the hard disk. If your application caches data, give the user some control over it. Include options in the startup application such as setting a limit on the maximum amount of cached data that will be stored on the hard disk, or having the application discard any cached data when it terminates. Even though a CD-ROM has an Autorun.inf file, AutoRun can be suppressed programmatically or disabled entirely with the registry. See Enabling and Disabling AutoRun for more information. -------------------------------------------------------------------------------- (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/autorun/autoplay_works.asp) ----- Autorun.inf Commands -------------------------------------------------------------------------------- This topic is a reference for the commands that can be used in an Autorun.inf file. [AutoRun] Commands [DeviceInstall] Commands [AutoRun] Commands icon label open shellexecute shell shell\verb icon The icon command specifies an icon which represents the AutoRun-enabled drive in the Microsoft® Windows® user interface. icon=iconfilename[,index] Parameters iconfilename Name of an .ico, .bmp, .exe, or .dll file containing the icon information. If a file contains more than one icon, you must also specify zero-based index of the icon. Remarks The icon, together with the label, represents the AutoRun-enabled drive in the Windows user interface. For instance, in Windows Explorer, the drive is represented by this icon instead of the standard drive icon. The icon's file must be in the same directory as the file specified by the open command. The following example specifies the second icon in the MyProg.exe file. icon=MyProg.exe,1 label The label command specifies a text label which represents the AutoRun-enabled drive in the Windows user interface. label=LabelText Parameters LabelText A text string containing the label. It can contain spaces. Remarks The label, together with an icon, represents the AutoRun-enabled drive in the Windows user interface. The following example specifies "My Drive Label" as the drive's label. label=My Drive Label open The open command specifies the path and file name of the application that AutoRun launches when a user inserts a disc in the drive. open=[exepath\]exefile [param1 [param2] ...] Parameters exefile Fully qualified path of an executable file that runs when the CD is inserted. If only a file name is specified, it must be in drive's root directory. To locate the file in a subdirectory, you must specify a path. You can also include one or more command-line parameters to pass to the startup application. Remarks For more information about startup applications, see Tips for Writing AutoRun Startup Applications. shellexecute Version 5.0. The shellexecute command specifies an application or data file that AutoRun will use to call ShellExecuteEx. shellexecute=[filepath\]filename[param1, [param2]...] Parameters filepath A string that contains the fully qualified path of the directory that contains the data or executable file. If no path is specified, the file must be in the drive's root directory. filename A string that contains the file's name. If it is an executable file, it is launched. If it is a data file, it must be a member of a file class. ShellExecuteEx launches the default command associated with the file class. paramx Contains any additional parameters that should be passed to ShellExecuteEx. Remarks This command is similar to open, but it allows you to use file association information to run the application. shell The shell command specifies a default command for the drive's shortcut menu. shell=verb Parameters verb The verb that corresponds to the command. The verb and its associated command must be defined in the Autorun.inf file with a shell\verb command. Remarks When a user right-clicks the drive icon, a shortcut menu appears. If an Autorun.inf file is present, the default shortcut menu command is taken from it. This command also executes when the user double-clicks the drive's icon. To specify the default shortcut menu command, first define its verb, command string, and menu text with shell\verb. Then use shell to make it the default shortcut menu command. Otherwise, the default menu item text will be "AutoPlay", which launches the application specified by the open command. shell\verb The shell\verb command adds a custom command to the drive's shortcut menu. shell\verb\command=Filename.exe shell\verb=MenuText Parameters verb The command's verb. The shell\verb\command command associates the verb with an executable file. Verbs must not contain embedded spaces. By default, verb is the text that is displayed in the shortcut menu. Filename.exe The path and file name of the application that performs the command. MenuText This parameter specifies the text that is displayed in the shortcut menu. If it is omitted, verb is displayed. MenuText can be mixed-case and can contain spaces. You can set a shortcut key for the menu item by putting an ampersand (&) in front of the letter. Remarks When a user right-clicks the drive icon, a shortcut menu appears. Adding shell/verb commands to the drive's Autorun.inf file allows you to add commands to this shortcut menu. There are two parts to this command, which must be on separate lines. The first part is shell/verb/command. It is required. It associates a string, called a verb, with the application to launch when the command runs. The second part is the shell/verb command. It is optional. You can include it to specify the text that displays in the shortcut menu. To specify a default shortcut menu command, define the verb with shell/verb, and make it the default command with shell. The following sample Autorun.inf fragment associates the readit verb with the command string "Notepad abc\readme.txt". The menu text is "Read Me", and 'M' is defined as the item's shortcut key. When the user selects this command, the drive's abc\readme.txt file opens with Microsoft Notepad. shell\readit\command=notepad abc\readme.txt shell\readit=Read &Me [DeviceInstall] Commands DriverPath DriverPath The DriverPath command specifies a directory to search recursively for driver files. This command is used during a driver installation and is not part of an AutoRun operation. The [DeviceInstall] section is only supported under Windows XP. [DeviceInstall] DriverPath=directorypath Parameters directorypath A path to a directory that Windows searches for driver files, along with all of its subdirectories. Remarks Do not use drive letters in directorypath as they change from one computer to the next. To search multiple directories, add a DriverPath command for each directory as in this example. [DeviceInstall] DriverPath=drivers\video DriverPath=drivers\audio To indicate that the media contains no driver files so that it is not searched at all, include a [DeviceInstall] section in Autorun.inf that contains no DriverPath commands. -------------------------------------------------------------------------------- (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/autorun/autoplay_reg.asp) ----- Enabling and Disabling AutoRun -------------------------------------------------------------------------------- There are many situations where AutoRun may need to be temporarily or persistently disabled. For example, AutoRun might interfere with the operation of a running application and need to be disabled for the duration. The system provides several ways to disable AutoRun. Suppressing AutoRun Programmatically Using the Registry to Disable AutoRun AutoRun for Other Types of Storage Media Suppressing AutoRun Programmatically There are a variety of situations where AutoRun may need to be suppressed programmatically. Two examples are: Your application has a setup program that requires the user to insert another disc that may contain an Autorun.inf file. During the operation of your application, the user may need to insert another disc that may contain an Autorun.inf file. In either case, you will normally not want to launch another application while the original is in progress. Users can manually suppress AutoRun by holding down the SHIFT key when they insert the CD-ROM. However, it is usually preferable to handle this operation programmatically rather than depending on the user. With systems that have Shell version 4.70 and later, Microsoft® Windows® sends a "QueryCancelAutoPlay" message to the foreground window. Your application can respond to this message to suppress AutoRun. This approach is used by system utilities such as the Open common dialog box to disable AutoRun. You will not get a "QueryCancelAutoPlay" message with versions of Windows 95 that do not have the Microsoft Internet Explorer 4.0 integrated Shell installed. The following code fragments illustrate how to set up and handle this message. Your application must be running in the foreground window. First, register "QueryCancelAutoPlay" as a Windows message: uMessage = RegisterWindowMessage(TEXT("QueryCancelAutoPlay")); Your application's window must be in the foreground to receive this message. The message handler should return TRUE to cancel AutoRun and FALSE to enable it. The following code fragment illustrates how to use this message to disable AutoRun. UINT g_uQueryCancelAutoPlay = 0; LRESULT WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { ... default: if (!g_uQueryCancelAutoPlay) { g_uQueryCancelAutoPlay = RegisterWindowMessage(TEXT("QueryCancelAutoPlay")); } if (uMsg && uMsg == g_uQueryCancelAutoPlay) { return TRUE; // cancel auto-play } } } If your application is using a dialog box and needs to respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE. Instead, call SetWindowLong with nIndex set to DWL_MSGRESULT. Set the dwNewLong parameter to TRUE to cancel AutoRun, and FALSE to enable it. For example, the following sample dialog box procedure cancels AutoRun when it receives a "QueryCancelAutoPlay" message. UINT g_uQueryCancelAutoPlay = 0; BOOL DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { ... default: if (!g_uQueryCancelAutoPlay) { g_uQueryCancelAutoPlay = RegisterWindowMessage(TEXT("QueryCancelAutoPlay")); } if (uMsg == g_uQueryCancelAutoPlay) { SetWindowLong(hDlg, DWL_MSGRESULT, TRUE); return 1; } } } Using the Registry to Disable AutoRun There are two registry values that can be used to persistently disable AutoRun: NoDriveAutoRun and NoDriveTypeAutoRun. The first value disables AutoRun for specified drive letters and the second disables AutoRun for a class of drives. If either of these values is set to disable AutoRun for a particular device, it will be disabled. Note The NoDriveAutoRun and NoDriveTypeAutoRun values should only be modified by system administrators to change the value for the entire system for testing or administrative purposes. Applications should not modify these values, as there is no way to reliably restore them to their original values. The NoDriveAutoRun value disables AutoRun for specified drive letters. It is a REG_DWORD data value, found under the following key: HKEY_CURRENT_USER Software Microsoft Windows CurrentVersion Policies Explorer The first bit of the value corresponds to drive A:, the second to B:, and so on. To disable AutoRun for one or more drive letters, set the corresponding bits. For example, to disable the A: and C: drives, set NoDriveAutoRun to 0x00000005. The NoDriveTypeAutoRun value disables AutoRun for a class of drives. It is a REG_DWORD or 4-byte REG_BINARY data value, found under the same key. HKEY_CURRENT_USER Software Microsoft Windows CurrentVersion Policies Explorer By setting the bits of this value's first byte, different drives can be excluded from working with AutoRun. The following table gives the bits and bitmask constants, that can be set in the first byte of NoDriveTypeAutoRun to disable AutoRun for a particular drive type. For Microsoft Windows NT® and later systems, you must restart Windows Explorer before the changes take effect. Bit Number Bitmask Constant Description 0x04 DRIVE_REMOVEABLE Disk can be removed from drive (such as a floppy disk). 0x08 DRIVE_FIXED Disk cannot be removed from drive (a hard disk). 0x10 DRIVE_REMOTE Network drive. 0x20 DRIVE_CDROM CD-ROM drive. 0x40 DRIVE_RAMDISK RAM disk. AutoRun for Other Types of Storage Media AutoRun is primarily intended for public distribution of applications on CD-ROM and DVD-ROM. However, it is often useful to enable AutoRun on other types of removable storage media. This feature is typically used simplify the debugging of AutoRun.inf files. AutoRun only works on removable storage devices when the following criteria are met: The device must have AutoRun-compatible drivers. To be AutoRun-compatible, a driver must notify the system that a disk has been inserted by sending a WM_DEVICECHANGE message. The root directory of the inserted media must contain an Autorun.inf file. The device must not have AutoRun disabled through the registry. The foreground application has not suppressed AutoRun. Note This feature should not be used to distribute applications on floppy disks. Because implementing AutoRun on a floppy disk provides an easy way to spread computer viruses, users should be suspicious of any publicly distributed floppy disk that contains an Autorun.inf file. Normally, AutoRun starts automatically, but it can also be started manually. If the device meets the criteria listed above, the drive letter's shortcut menu will include an AutoPlay command. To run AutoRun manually, either right-click the drive icon and select AutoPlay from the shortcut menu or double-click the drive icon. If the drivers are not AutoRun-compatible, the shortcut menu will not have an AutoPlay item and AutoRun cannot be started. AutoRun-compatible drivers are provided with some floppy disk drives, as well as some other types of removable media such as CompactFlash cards. AutoRun also works with network drives that are mapped to a drive letter with Windows Explorer or mounted with the Microsoft Management Console (MMC). As with mounted hardware, a mounted network drive must have an Autorun.inf file in its root directory, and must not be disabled through the registry. --------------------------------------------------------------------------------