I recently saw a number of friends in discussing how to lock the CD-ROM drive, but there is no result.
Take the weaker, write the code and share it with everyone.
Create a new project, join two buttons on the form, paste the following procedures, how about the effect? ^ _ ^
Option ExplicitPrivate Const GENERIC_READ As Long = & H80000000Private Const FILE_SHARE_READ As Long = & H1Private Const FILE_SHARE_WRITE As Long = & H2Private Const OPEN_EXISTING As Long = 3Private Const IOCTL_STORAGE_MEDIA_REMOVAL As Long = & H2D4804
Private type prevent_media_removal preventmediaremoval as byteend type
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 Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32" (Byval Hobject As Long) AS Long
Public Function LockCDROM (szDrive As String, IsLock As Boolean) As Boolean On Error GoTo Err Dim hDevice As Long Dim PMR As PREVENT_MEDIA_REMOVAL Dim bytesReturned As Long Dim Success As Long hDevice = CreateFile ( "//./" & szDrive, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0 &, OPEN_EXISTING, 0 &, 0 &) PMR.PreventMediaRemoval = CByte (Abs (IsLock)) Success = DeviceIoControl (hDevice, IOCTL_STORAGE_MEDIA_REMOVAL, PMR, Len (PMR), ByVal 0 &, 0 &, bytesReturned, ByVal 0 &) CloseHandle hDevice Lockcdrom = true exit functionerr: lockcdrom = false functionprivate submmand1_click () DIM RET AS BOOLEAN RET = LOCKCDROM ("H:, true) 'My optical drive letter is h: if return dam" lock success ", vbinformation," Tip "Else MsgBox" Lock Fail ", Vbinformation," Tips "end ifend sub
Private Sub Command2_Click () DIM RET As Boolean Ret = LockCDROM ("H:", FALSE) IF RET THEN MSGBOX "Unlock Success", Vbinformation, "Tips" ELSE MSGBOX "Unlock", Vbinformation, "Tips" end iFend Sub