Results 1 to 20 of 20

Thread: keep cdrom close

Threaded View

  1. #19
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: keep cdrom close

    staring me in the face, very much like the code to lock the drive above:

    VB Code:
    1. Private Const INVALID_HANDLE_VALUE As Short = -1
    2.     Private Const OPEN_EXISTING As Short = 3
    3.     Private Const FILE_ATTRIBUTE_NORMAL As Short = &H80S
    4.     Private Const FILE_SHARE_READ As Short = &H1S
    5.     Private Const FILE_SHARE_WRITE As Short = &H2S
    6.     Private Const GENERIC_READ As Integer = &H80000000
    7.  
    8.     'Similar to the one to lock the drive above, but we send this message:
    9.     Private Const IOCTL_STORAGE_EJECT_MEDIA = &H2D4808
    10.     'oh, load aswell whilst I'm here...
    11.     Private Const IOCTL_STORAGE_LOAD_MEDIA = &H2D480C
    12.  
    13.     Private Declare Function DeviceIoControl Lib "kernel32" _
    14.     (ByVal hDevice As Integer, _
    15.      ByVal dwIoControlCode As Integer, _
    16.      ByRef lpInBuffer As Object, _
    17.      ByVal nInBufferSize As Integer, _
    18.      ByRef lpOutBuffer As Object, _
    19.      ByVal nOutBufferSize As Integer, _
    20.      ByRef lpBytesReturned As Integer, _
    21.      ByRef lpOverlapped As OVERLAPPED) As Integer
    22.  
    23.     'We use CreateFile to get a handle to the drive
    24.     Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
    25.             (ByVal lpFileName As String, _
    26.             ByVal dwDesiredAccess As Integer, _
    27.             ByVal dwShareMode As Integer, _
    28.             ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    29.             ByVal dwCreationDisposition As Integer, _
    30.             ByVal dwFlagsAndAttributes As Integer, _
    31.             ByVal hTemplateFile As Integer) As Integer
    32.  
    33.     'Required structure, wil be passed blank
    34.     Private Structure SECURITY_ATTRIBUTES
    35.         Dim nLength As Integer
    36.         Dim lpSecurityDescriptor As Integer
    37.         Dim bInheritHandle As Integer
    38.     End Structure
    39.  
    40.     'Close the handle we got by using CreateFile to open the drive
    41.     Private Declare Function CloseHandle Lib "kernel32" ( _
    42.             ByVal hObject As Integer) As Integer
    43.  
    44.     'Required structure, wil be passed blank
    45.     Private Structure OVERLAPPED
    46.         Public Internal As Integer
    47.         Public InternalHigh As Integer
    48.         Public offset As Integer
    49.         Public OffsetHigh As Integer
    50.         Public hEvent As Integer
    51.     End Structure
    52.  
    53.     Private Function EjectMedia(ByRef DriveLetter As String) As Boolean
    54.         'convert drive letter to a device path
    55.         Dim FullDrivePath As String = "//./" & DriveLetter
    56.         Dim hDrive As Integer
    57.  
    58.         hDrive = CreateFile(FullDrivePath, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
    59.         New SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
    60.  
    61.         Dim lpBytesReturned As Integer
    62.         If hDrive <> INVALID_HANDLE_VALUE Then
    63.             EjectMedia = CBool(DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, _
    64.             0, 0, 0, 0, lpBytesReturned, New OVERLAPPED))
    65.             Call CloseHandle(hDrive)
    66.         End If
    67.     End Function
    68.  
    69.     Private Function LoadMedia(ByRef DriveLetter As String) As Boolean
    70.         'convert drive letter to a device path
    71.         Dim FullDrivePath As String = "//./" & DriveLetter
    72.         Dim hDrive As Integer
    73.  
    74.         hDrive = CreateFile(FullDrivePath, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
    75.         New SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
    76.  
    77.         Dim lpBytesReturned As Integer
    78.         If hDrive <> INVALID_HANDLE_VALUE Then
    79.             LoadMedia = CBool(DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, _
    80.             0, 0, 0, 0, lpBytesReturned, New OVERLAPPED))
    81.             Call CloseHandle(hDrive)
    82.         End If
    83.     End Function
    84.  
    85.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    86.         EjectMedia("D:")
    87.     End Sub
    88.  
    89.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    90.         LoadMedia("D:")
    91.     End Sub

    edit: added load_media as well, but I can't test it as I'm on a laptop that can't load cds itself. Now I just need to combine the lot, to programatically eject and load, and fire events when the user ejects or loads or adds a usb drive. It would be a useful class.
    Last edited by jo0ls; Oct 12th, 2005 at 05:51 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width