Mega Code Archive

 
Categories / Delphi / Multimedia
 

Testingsetting the CD autorun option under Win32

Title: Testing/setting the CD autorun option under Win32 Question: How can I test and set the CD autorun option under 32 bit versions of Windows? Answer: You can check the 4 byte value in the Windows registry under: Software\Microsoft\Windows\CurrentVersion\Policies\ExplorerNoDriveTypeAutoRun Setting the following bit numbers of the value of this key will cause Windows not to use any auto run capabilities that may be present for the device denoted by the bit number. DRIVE_UNKNOWN = bit # 0 DRIVE_NO_ROOT_DIR = bit #1 DRIVE_REMOVABLE= Bit # 2 DRIVE_FIXED = Bit #3 DRIVE_REMOTE= Bit #4 DRIVE_CDROM= Bit #5 DRIVE_RAMDISK= Bit #6 You can also set this value to test the autorun capability of a program you are developing from a floppy or network drive, by setting the value to boot the autorun program from a floppy disk (DRIVE_REMOVEABLE) or network drive (DRIVE_REMOTE). If the device does not have auto-insert detection capabilities, simply inserting the disk and pressing F-5 from the Windows explorer should cause the autorun program to boot. The following example demonstrates the ability to check if the autorun detection of the CD is enabled, and also demonstrates the ability of turning the autorun detection both on and off for the CD. Example uses Registry; function IsCdAutoRunOn : bool; var reg: TRegistry; AutoRunSetting : integer; begin reg := TRegistry.Create; reg.RootKey := HKEY_CURRENT_USER; reg.OpenKey( 'Software\Microsoft\Windows\CurrentVersion\Policies\Explorer', false); reg.ReadBinaryData('NoDriveTypeAutoRun', AutoRunSetting , sizeof(AutoRunSetting)); reg.CloseKey; reg.free; result := not ((AutoRunSetting and (1 shl 5)) 0); end; procedure SetCdAutoRun(bOn : bool); var reg: TRegistry; AutoRunSetting : integer; begin reg := TRegistry.Create; reg.RootKey := HKEY_CURRENT_USER; reg.LazyWrite := false; reg.OpenKey( 'Software\Microsoft\Windows\CurrentVersion\Policies\Explorer', false); reg.ReadBinaryData('NoDriveTypeAutoRun', AutoRunSetting , sizeof(AutoRunSetting)); if bOn then AutoRunSetting := AutoRunSetting and not (1 shl 5) else AutoRunSetting := AutoRunSetting or (1 shl 5); reg.WriteBinaryData('NoDriveTypeAutoRun', AutoRunSetting , sizeof(AutoRunSetting)); reg.CloseKey; reg.free; end; procedure TForm1.Button1Click(Sender: TObject); begin if IsCdAutoRunOn then ShowMessage('CD AutoRun Is On') else ShowMessage('CD AutoRun Is Off'); end; procedure TForm1.Button2Click(Sender: TObject); begin SetCdAutoRun(True); end; procedure TForm1.Button3Click(Sender: TObject); begin SetCdAutoRun(False); end;