none
Eject CD-Rom on Button Click

    Question

  • Hello

    I am developing a vb.net application which have a buuton , i want when user clicks on Button the My pc's CD-ROM will be ejected...

    How can i dot this........

    Friday, April 13, 2007 6:16 PM

Answers

  • Translated from VB6 sample code, this popped out the CD-Rom on my laptop:

    Public Class Form1
      Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
      Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As IntPtr, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBufferSize As Integer, ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As IntPtr) As Integer
      Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer
      Private Const INVALID_HANDLE_VALUE As Integer = -1
      Private Const OPEN_EXISTING As Integer = 3
      Private Const GENERIC_READ As Integer = &H80000000
      Private Const GENERIC_WRITE As Integer = &H40000000
      Private Const IOCTL_STORAGE_EJECT_MEDIA As Integer = 2967560

      Private Sub EjectCDRom(ByVal driveletter As String)
        Dim path As String = "\\.\" + driveletter
        If Not path.EndsWith(":") Then path = path + ":"
        Dim hDrive As IntPtr = CreateFile(path, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
        If CInt(hDrive) = INVALID_HANDLE_VALUE Then Throw New IO.IOException("Could not open drive " + driveletter)
        Dim dummy As Integer
        DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dummy, IntPtr.Zero)
        CloseHandle(hDrive)
      End Sub

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EjectCDRom("d")
      End Sub
    End Class

    Friday, April 13, 2007 6:52 PM

All replies

  • Translated from VB6 sample code, this popped out the CD-Rom on my laptop:

    Public Class Form1
      Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
      Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As IntPtr, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBufferSize As Integer, ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As IntPtr) As Integer
      Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer
      Private Const INVALID_HANDLE_VALUE As Integer = -1
      Private Const OPEN_EXISTING As Integer = 3
      Private Const GENERIC_READ As Integer = &H80000000
      Private Const GENERIC_WRITE As Integer = &H40000000
      Private Const IOCTL_STORAGE_EJECT_MEDIA As Integer = 2967560

      Private Sub EjectCDRom(ByVal driveletter As String)
        Dim path As String = "\\.\" + driveletter
        If Not path.EndsWith(":") Then path = path + ":"
        Dim hDrive As IntPtr = CreateFile(path, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
        If CInt(hDrive) = INVALID_HANDLE_VALUE Then Throw New IO.IOException("Could not open drive " + driveletter)
        Dim dummy As Integer
        DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dummy, IntPtr.Zero)
        CloseHandle(hDrive)
      End Sub

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EjectCDRom("d")
      End Sub
    End Class

    Friday, April 13, 2007 6:52 PM
  • Hello,

    Can u plz tell me or explain me wht u have replyed... like Wht are Constant here why we define them ?? Is the name of the const can't be chnage.???

     

    Or If is there any other easy way of doing the same..???

    Friday, April 13, 2007 8:42 PM
  • I'm using P/Invoke to call native Windows API functions.  It needs to be done this way, the framework has no support for ejecting CD-Roms.  Just copy and paste the code, it will most likely work...
    Friday, April 13, 2007 9:04 PM
  • You said that you translated the code from VB6; Could you possibly give me the code for use with visual basic 6?

    Saturday, March 15, 2008 4:18 AM