Suppressing AutoPlay

There are a variety of reasons why you might want to suppress AutoPlay. One might be that your application has a setup program that requires the user to insert a disc which contains an Autorun.inf file. In this case, you would not want the AutoPlay feature to begin running an application while your setup application is running.

Another reason to suppress AutoPlay might be that your user needs to do disk swapping during your application. You would probably not want the setup program to execute while your application is in progress.

You can manually prevent the Autorun.inf file on a compact disc from being parsed and carried out by holding down the SHIFT key when you insert the disc.

Users of Windows NT version 4.0 can suppress AutoPlay automatically using the following code example. This initialization code is based on the assumption that your setup application is in the foreground window.

uMessage - RegisterWindowMessage(TEXT("QueryCancelAutoPlay"));

Then, add the following code to your setup window procedure:

if(msg == uMessage)

{

// return 1 to cancel AutoPlay

// return 0 to allow AutoPlay

return 1L;

}

Windows 95 users can suppress AutoPlay by setting the value of the NoDriveTypeAutoRun registry entry to 0xFF. The following code sample demonstrates how this can be done. Insert this code near the beginning of the WinMain function.

const unsigned long WINDOWS_DEFAULT_AUTOPLAY_VALUE=0x095;

const unsigned long AUTOPLAYOFF=0x0FF;

unsigned long ulOldAutoRunValue = WINDOWS_DEFAULT_AUTOPLAY_VALUE;

unsigned long ulDisableAutoRun = AUTOPLAYOFF;

unsigned long ulDataSize = sizeof(unsigned long);

HKEY hkey = NULL;

char *lpzRegistryString = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"

if( RegOpenKeyEx(HKEY_CURRENT_USER,

lpzRegistryString ,

0, KEY_ALL_ACCESS, &hkey ) == ERROR_SUCCESS )

{

if( RegQueryValueEx(hkey,"NoDriveTypeAutoRun", 0, NULL,

(unsigned char*)&ulOldAutoRunValue,

&ulDataSize ) == ERROR_SUCCESS )

{

RegSetValueEx(hkey, "NoDriveTypeAutoRun", 0, REG_BINARY,

(const unsigned char*)&ulDisableAutoRun, 4);

}

else ulOldAutoRunValue = WINDOWS_DEFAULT_AUTOPLAY_VALUE;

RegFlushKey( hkey );

RegCloseKey( hkey );

}

Just before your application terminates, it must reset the registry to the old value, as shown in the code example below:

// Restore original AutoPlay settings.

if (RegOpenKeyEx(HKEY_CURRENT_USER,

lpzRegistryString ,

0, KEY_ALL_ACCESS, &hkey ) == ERROR_SUCCESS )

{

RegSetValueEx(hkey, "NoDriveTypeAutoRun", 0, REG_BINARY,

(const unsigned char*)&ulOldAutoRunValue,

4 );

RegFlushKey( hkey );

RegCloseKey( hkey );

}