Heres how to do it...
You need to send IOCTL_STORAGE_MEDIA_REMOVAL using the DeviceIOControl Function, use true to lock, false to unlock.
See the msdn library for details.

Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0.

VB Code:
  1. Private _locked As Boolean
  2.     Private Const INVALID_HANDLE_VALUE As Short = -1
  3.     Private Const OPEN_EXISTING As Short = 3
  4.     Private Const FILE_ATTRIBUTE_NORMAL As Short = &H80S
  5.     Private Const FILE_SHARE_READ As Short = &H1S
  6.     Private Const FILE_SHARE_WRITE As Short = &H2S
  7.     Private Const GENERIC_READ As Integer = &H80000000
  8.     Private Const IOCTL_STORAGE_MEDIA_REMOVAL As Integer = 2967556
  9.  
  10.     'We use CreateFile to get a handle to the drive
  11.     Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
  12.             (ByVal lpFileName As String, _
  13.             ByVal dwDesiredAccess As Integer, _
  14.             ByVal dwShareMode As Integer, _
  15.             ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
  16.             ByVal dwCreationDisposition As Integer, _
  17.             ByVal dwFlagsAndAttributes As Integer, _
  18.             ByVal hTemplateFile As Integer) As Integer
  19.  
  20.     'We use this to send PREVENT_MEDIA_REMOVAL to the drive using the handle
  21.     Private Declare Function DeviceIoControl Lib "kernel32" ( _
  22.             ByVal hDevice As Integer, _
  23.             ByVal dwIoControlCode As Integer, _
  24.             ByRef lpInBuffer As Boolean, _
  25.             ByVal nInBufferSize As Integer, _
  26.             ByRef lpOutBuffer As Integer, _
  27.             ByVal nOutBufferSize As Integer, _
  28.             ByRef lpBytesReturned As Integer, _
  29.             ByRef lpOverlapped As OVERLAPPED) As Integer
  30.  
  31.  
  32.     'Required structure, wil be passed blank
  33.     Private Structure OVERLAPPED
  34.         Public Internal As Integer
  35.         Public InternalHigh As Integer
  36.         Public offset As Integer
  37.         Public OffsetHigh As Integer
  38.         Public hEvent As Integer
  39.     End Structure
  40.  
  41.     'Required structure, wil be passed blank
  42.     Private Structure SECURITY_ATTRIBUTES
  43.         Dim nLength As Integer
  44.         Dim lpSecurityDescriptor As Integer
  45.         Dim bInheritHandle As Integer
  46.     End Structure
  47.  
  48.     'Close the handle we got by using CreateFile to open the drive
  49.     Private Declare Function CloseHandle Lib "kernel32" ( _
  50.             ByVal hObject As Integer) As Integer
  51.  
  52.     Private Function LockMedia(ByRef DriveLetter As String, ByRef Lock As Boolean) As Boolean
  53.         'convert drive letter to a device path
  54.         Dim FullDrivePath As String = "//./" & DriveLetter
  55.         Dim hDrive As Integer
  56.  
  57.         hDrive = CreateFile(FullDrivePath, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
  58.         New SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  59.  
  60.         If hDrive <> INVALID_HANDLE_VALUE Then
  61.             LockMedia = CBool(DeviceIoControl(hDrive, IOCTL_STORAGE_MEDIA_REMOVAL, _
  62.             Lock, 1, 0, 0, New Integer, New OVERLAPPED))
  63.  
  64.             Call CloseHandle(hDrive)
  65.         End If
  66.     End Function
  67.  
  68.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  69.  
  70.         If _locked Then
  71.             'pass the drive letter and a colon, then true to lock, false to unlock.
  72.             'if you lock n times then you have to unlock n times to get control back.
  73.  
  74.             LockMedia("E:", False)
  75.             Button1.Text = "Lock"
  76.             Me.Text = "Drive E is unlocked"
  77.         Else
  78.             LockMedia("E:", True)
  79.             Button1.Text = "Unlock"
  80.             Me.Text = "Drive E is locked"
  81.         End If
  82.         _locked = Not _locked
  83.  
  84.     End Sub