Visual Basic Disk/Drive API Routines
mciSendString: Ejecting Media from a CD-ROM
     
Posted:   Sunday June 21, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

GetDriveType: Identify a System's CD-ROM Drive
DeviceIoControl: Load/Eject Removable Media
DeviceIoControl: Lock/Unlock Removable Media Devices
     
 Prerequisites
None.

The Multimedia functions provide several commands via the mciSendString API. Perhaps one of the most frequently requested is ejecting (and loading) a CD automatically from code. Using a single line call, this is easily achieved.
 BAS Module Code
None.

 Form Code
Drop a command button onto a form and add the following:

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.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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


Private Sub Command1_Click()

   Call mciSendString("Set CDAudio Door Open Wait", 0&, 0&, 0&)

End Sub
 Comments
Pretty simple -- the API takes a pre-defined string and executes it.  The return value is 0 if successful, or an error code if not. And unlike its sister the mciSendCommand api, there is no need to obtain a handle to the device. However, on systems with multiple CD-ROMs installed, this may be the only method to assure the correct CD is ejected. But since I have only one here, I can't test this out.

The following table, from the MSDN reference, lists device types that recognize the set command and the flags used by each type:

cdaudio audio all off
audio all on
audio left off
audio left on
audio right off
audio right on
door closed
door open
time format milliseconds
time format msf
time format tmsf
digitalvideo     audio all off
audio all on
audio left off
audio left on
audio right off
audio right on
door closed
door open
file format format
seek exactly on
seek exactly off
speed factor
still file format format
time format frames
time format milliseconds
video off
video on
overlay audio all off
audio all on
audio left off
audio left on
audio right off
audio right on
door closed
door open
video off
video on
sequencer audio all off
audio all on
audio left off
audio left on
audio right off
audio right on
door closed
door open
master MIDI
master none
master SMPTE
offset time
port mapper
port none
port port_number
slave file
slave MIDI
slave none
slave SMPTE
tempo tempo_value
time format milliseconds
time format SMPTE fps
time format SMPTE 30 drop
time format song pointer
vcr assemble record on
assemble record off
audio all off
audio all on
audio left off
audio left on
audio right off
audio right on
clock time
counter format
counter value
door closed
door open
index counter
index date
index time
index timecode
length duration
pause timeout
postroll duration -
duration
power on
power off
preroll duration duration
record format SP
record format LP
record format EP
speed factor
time format frames
time format hms
time format milliseconds
time format msf
time format SMPTE fps
time format SMPTE 30 drop
time format tmsf
time mode counter
time mode detect
time mode timecode
tracking plus
tracking minus
tracking reset
videodisc audio all off
audio all on
audio left off
audio left on
audio right off
audio right on
door closed
door open
time format frames
time format hms
time format milliseconds
time format track
video off
video on
waveaudio alignment integer
any input
any output
audio all off
audio all on
audio left off
audio left on
audio right off
audio right on
bitspersample bit_count
bytespersec byte_rate
channels channel_count
door closed
door open
format tag pcm
format tag tag
input integer
output integer
samplespersec integer
time format bytes
time format milliseconds
time format samples

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter