Lock Unlock Removable Media
Lock Unlock Removable Media
htm
Prerequisites
Windows NT, Windows 2000, Windows XP
the standard device input/output control codes are available only on Windows NT/2000/XP.
on Windows 95/98, an application must specify a virtual device driver in the CreateFile
function—not a specific device.
This demo is based on code provided by Mattias Sjögren in the Microsoft newsgroups.
BAS Module Code
Place the following code into the general declarations area of a bas module:
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const DRIVE_REMOVABLE As Long = 2
Public Const DRIVE_CDROM As Long = 5
Public Const INVALID_HANDLE_VALUE As Long = -1&
Public Const GENERIC_READ As Long = &H80000000
Public Const FILE_SHARE_READ As Long = &H1
Public Const FILE_SHARE_WRITE As Long = &H2
Public Const FILE_ANY_ACCESS As Long = &H0
Public Const FILE_READ_ACCESS As Long = &H1
Public Const FILE_WRITE_ACCESS As Long = &H2
Public Const OPEN_EXISTING As Long = 3
Public Const IOCTL_STORAGE_MEDIA_REMOVAL As Long = &H2D4804
End If
Call CloseHandle(hDevice)
DeviceLock = success <> 0
End Function
Add a listbox (List1), a command button array (Command1(0), Command1(1)), and a label (Label1)
to a form. The third button shown (End) is optional. Add the following code to the form:
Option Explicit
End Sub
End Sub
'display result
If result Then
Else
Label1.Caption = "Call failed - perhaps no media in device."
End If
End If
End Sub
If (drvType = DRIVE_CDROM) Or _
(drvType = DRIVE_REMOVABLE) Then
lst.AddItem currDrive
End If
Loop
End Sub
If pos Then
End If
End Function
End If
End Function
Comments
Save the program and run. Once you've locked the media, activating the drive panel eject button or
Windows context menu command will not eject the media from the device.
PayPal Link
Like what you see here? Help ensure continued VB
Classic
development by making a small PayPal donation
today. Thank you.