Link to home
Start Free TrialLog in
Avatar of Zocko2000
Zocko2000

asked on

How to eject and load any CD drive

I've found this nice code to eject any CD in different drives. But how I must modify this code to reload the CD in the device ???

Could anybody help ???

Thanks

Michael



Private Type Registers
    RegBX As Long
    RegDX As Long
    RegCX As Long
    RegAX As Long
    RegDI As Long
    RegSI As Long
    RegFlags As Long
End Type

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Boolean
End Type

Private Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
Private Const VWin32_DIOC_DOS_IOCTL = 1

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, ByVal lpOverlapped As Long) As Long

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, 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 EjectMedia(Drive As String) As Boolean
    Dim SecAttr As SECURITY_ATTRIBUTES
    Dim ErrorResult
    Dim hDevice As Long, Regs As Registers, RB As Long
   
    EjectMedia = False
    hDevice = CreateFile("\\.\vwin32", 0, 0, SecAttr, 0, FILE_FLAG_DELETE_ON_CLOSE, 0)
    If hDevice = -1 Then Exit Function
    With Regs
        .RegAX = &H220D
        .RegBX = Asc(Left$(UCase$(Drive), 1)) - 64
        .RegCX = &H849
    End With
    DeviceIoControl hDevice, VWin32_DIOC_DOS_IOCTL, Regs, Len(Regs), Regs, Len(Regs), RB, 0
    CloseHandle hDevice
    If Regs.RegAX = 0 Then EjectMedia = True
End Function
Avatar of visualfool
visualfool

I think i saw something at http://616.org
Here is some code I have made for my new Tip for my new vb site shows you how to open and close cd door

'-------Code Starts Here--------------'

                                                        How to Open and close the CD door

Use with VB
Works with VB5, VB4, VB6

Copy and paste in to a new module named module1


--------------------------------------------------------------------------------

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Enum OpenClose
 OpenDoor = 1
 CloseDoor = 2
End Enum

Function cdDoor(TCommand As OpenClose)
Select Case TCommand
Case 1
Call mciSendString("set cdaudio door open", 0, 0, 0)
Case 2
Call mciSendString("set cdaudio door closed", 0, 0, 0)
End Select

End Function

Place two command buttons a on new form and paste the code into the General Declarations selection and press F5

Private Sub Command1_Click()
Module1.cdDoor OpenDoor

End Sub

Private Sub Command2_Click()
Module1.cdDoor CloseDoor

End Sub

Private Sub Form_Load()
Command1.Caption = "Open Door"
Command2.Caption = "Close Door"

End Sub
What would be alot easier is to use an MCI control. It needs to be added to a project in order to be used. It looks like the buttons on a VCR or tape player. This control has a method that will eject and retract the cd tray.
I think that the control can be loaded by checking the box for "Microsoft MCI Control" Components window. (Click on components in the menu.)
Zocko2000,

That code looks *REALLY* familiar... Did you happen to get that code here: http://www1.experts-exchange.com/EQ.10288603 

I wrote that code, and if you didn't get it there, I would be really curious to know where you got it...

Anyhow, back to the code.  It was written using the FAT32 File System functions originally for ejecting JAZ and ZIP Drive media, however the code will also eject CDs. Take a look at this microsoft URL: http://msdn.microsoft.com/library/psdk/win95/fat32ovr_8h6b.htm 
Looking at this article, you will see that there is a "Eject Removable Media" function, but there is not a "Load Removable Media" function...


Cheers!®©
ASKER CERTIFIED SOLUTION
Avatar of cys050200
cys050200

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zocko2000

ASKER

Thank you for your help !

Zocko