How to lock CD autorun in Windows XP
If we write a program that should read CD or DVD, the question is what to do with jumping window, in which Windows XP repeatedly asks what it should do with the disk.
Windows allows us, with the help of API provided by it, to transfer the control of CD drive completely to our program. Then the pop-up window will not appear. It's good. On the other hand, if we transfer the control, we will not be able to work with file system on the disk using the operators of programming language. The access to the disk will be possible only through the API. This is bad, because it is uncomfortable.
Since the appearance of the autorun window is just the answer of Windows to closing of the tray of CD drive, the solution of the problem is obvious: to lock the CD-drive for all time when the tray is open, release the lock after the fact that our program, through the API, receives a signal that the tray is closed. Further, since the lock does not exist anymore, the access to the files on the disk will be possible with the help of operators of programming language (in our case Visual Basic 6).
You can see below the program realization on VB6.
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _ (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, _ ByVal lpSecurityAttributes As Long, _ ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, _ ByVal hTemplateFile As Long) As Long Dim hfile As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _ (ByVal lpstrCommand As String, _ ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, _ ByVal hwndCallback As Long) As Long Dim lRet As Long Dim ReturnString As String Private Declare Function DeviceIoControl Lib "kernel32" ( _ ByVal hDevice As Long, _ ByVal dwIoControlCode As Long, _ lpInBuffer As Any, _ ByVal nInBufferSize As Long, _ lpOutBuffer As Any, _ ByVal nOutBufferSize As Long, _ lpBytesReturned As Long, _ lpOverlapped As Overlapped) As Long Private Type Overlapped 'It is for DeviceIoControl pleasure Internal As Long InternalHigh As Long offset As Long OffsetHigh As Long hEvent As Long End Type Dim BytesReturned As Long Dim structOVERLAPPED As Overlapped Private Const GENERIC_READ As Long = &H80000000 Private Const FILE_SHARE_READ As Long = 1 Private Const FILE_SHARE_WRITE As Long = 2 Private Const OPEN_EXISTING As Long = 3 Private Const FSCTL_LOCK_VOLUME As Long = &H90018 Private Const IOCTL_STORAGE_EJECT_MEDIA As Long = 2967560 Private Const IOCTL_STORAGE_CHECK_VERIFY As Long = &H2D4800 Private Sub mFileOpen_Click() ' StartPath is a path which user selected DriveType = GetDriveType(StartPath) If DriveType = 5 Then 'If DriveType = 5 this is CD-drive hfile = CreateFile("\\.\" & StartPath, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0) 'We get the handle lRet = DeviceIoControl(hfile, FSCTL_LOCK_VOLUME, Null, 0, Null, 0, BytesReturned, structOVERLAPPED) 'We lock the CD-drive lRet = DeviceIoControl(hfile, IOCTL_STORAGE_EJECT_MEDIA, Null, 0, Null, 0, BytesReturned, structOVERLAPPED) 'We open the CD-drive door TimerCheckkDisk.Enabled = True 'We will wait for CD-drive door clothing in that timer Else If hfile <> -1 Then CloseHandle (hfile) 'If a handle is opened we close the handle (and unlock the CD-drive by means this action) lRet = mciSendString("set cdaudio door closed", ReturnString, 255, Me.hWnd) 'We close the CD-drive door TimerCheckkDisk.Enabled = False ' THIS IS THE POINT WHERE WE CAN START WORK WITH HARD DRIVE End If End Sub Private Sub TimerCheckkDisk_Timer() lRet = DeviceIoControl(hfile, IOCTL_STORAGE_CHECK_VERIFY, Null, 0, Null, 0, BytesReturned, structOVERLAPPED) 'We check status of the cd-drive door If lRet <> 0 Then CloseHandle (hfile) 'We close the handle (and unlock the CD-drive by means this action) TimerCheckkDisk.Enabled = False TimerCheckDir.Enabled = True End If End Sub Private Sub TimerCheckDir_Timer() On Error GoTo ExitSub VolumeName = Dir(StartPath + "\", vbVolume) TimerCheckDir.Enabled = False ' THIS IS THE POINT WHERE WE CAN START WORK WITH INSERTED CD-DVD-DISK ExitSub: End Sub
Table of Contents
|© Max Petrov||При использовании материалов ссылка на sadda.ru обязательна|