http://www.chrispearson.org/pages/articles/CDBG.asp
09h39
Wednesday, 8. October 2008

AUTOPLAY CD

The autoplay feature in Windows is controlled by the contents of a file called autorun.inf on the CD itself and by registry settings on the PC. The autorun.inf file determines which icon is displayed in Windows Explorer, what menu options are available when the CD icon is right-clicked and, most importantly, what happens when autoplay is initiated.

The registry settings determine whether or not autoplay is enabled for specified devices.

The autorun.inf file

At its most simple the autorun.inf file can contain just a reference to the icon which is displayed against the disc in Windows Explorer. In this instance the content of the file would be

[autorun]
ICON=smile.ico

The name of the icon can refer to any valid icon graphic. Notice that - in Windows - the case of text in autorun.inf is irrelevant.

If you want to add options to the pop-up menu which appears when the CD icon is right-clicked, options can be added by inserting pairs of rows to autorun.inf, the first line specifying the text which will be displayed and the second the action Windows should take. In the example below we need the menu item to read Instal printer test and the action to take is to install our printer test program from the CD.

[autorun]
ICON=smile.ico

shell\mylabel Instal printer test
shell\mylabel\command setup.exe

Finally, in the simple case, there is the small matter of what will be autoplayed. Quite a lot has been written about autorun's inability to transparently handle HTML files. It should be as easy as including

file=StartUp.html

or something similar in autorun.inf. But, if you've ever tried it, you'll have noticed that this doesn't work.

There have been a number of ways of getting round this shortcoming. Most rely on reading Windows' configuration from its registry and working with a known browser returned from the HKEY_CLASSES_ROOT\HTTP\shell\open\command registry entry. Beware, however, that not all browsers stick with the conventions used by IE and Netscape, if you're inclined to write your own launch software. There are a number of ready-written programs available which allow you to run them with the name of the HTML file passed as a command line argument, as in

runHTML.exe StartUp.htm

 


There is a wrinkle you can exploit, in launching a browser through a file-type association. This can be scripted or, at its most basic, run from a couple of batch files. Ordinarily I try to avoid interpreted, plain text files like .bat but, since a CD is read-only, there is little chance of the end user, however malevolent, vandalising the files.

It is this technique that's used in the article on creating an autoplay CD and which is the only practical step that doesn't involve downloading and using a third-party executable or demonstrating your own C++ proficiency.

Creating an autoplay CD - Main article Top of this page

 

The line in autorun.inf which runs this is

open=Run_StartHTML.bat

Notice that this line runs a .bat which, in turn, runs another .bat We pass the name of the HTML file to the first batch file as a command line argument. The first batch file passes the file name to the second as a parameter, which invokes the file association between a .htm file extension (could alternatively be .html if you prefer) and the user's browser.

The batch files are

Run_StartHTML.bat

StartHTML.bat
startHTML.bat startup.HTM
exit
start %1
exit
 

The final file

The complete autorun.inf file is now

[autorun]
ICON=smile.ico
shell\my label Instal printer test
shell\mylabel\command setup.exe
open=Run_StartHTML.bat

 

In these examples all the files have been dumped together in the root folder. This has been done mostly to keep things simple and, with less than half a dozen files, folders are something of a luxury you can live without. It's preferable (tidier, anyway) to put autorun.inf in the CD's root and place everything else in one or more appropriate folders. For instance, autorun.inf in the root, autoplay files in a folder called autoplay or autorun and the remaining CD content in a file system in the path \Content\ or similar.

To work, autoplay.inf must be in the root folder. If it's not, Windows will ignore it.

Bear in mind that, using HTML, links are all relative to the folder in which the currently-displayed file is stored, so you don't need to ascertain which drive letter the user has associated with the CD drive.

There is a lot of background information on autoplay on the MSDN web site:
Search for autoplay CD
Top of this page

xxx,xxx

copyright ©2000 - 2008 Chris Pearson