Does anyone know a method to programmatically close the CD tray on Windows 2000 or higher? Open CD tray exists, but I can't seem to make it close especially under W2k.
I am especially looking for a method to do this from a batch file, if possible, but API calls would be OK.
The Eject key is usually located near the volume controls and is marked by a triangle pointing up with a line underneath. In Windows, search for and open File Explorer. In the Computer window, select the icon for the disc drive that is stuck, right-click the icon, and then click Eject. The disc tray should open.
The picture is a close-up example of an eject button and icon on the front of a CD-ROM drive. It's usually represented by a triangle with a line underneath and on the drive's right side.
If you suspect that a CD may be stuck in the drive, the tray needs to be ejected manually. Look for the small manual eject hole (not the headphone jack) on the front of the drive. Unwind a paper clip and place one end of the paper clip into the hole to eject the CD-ROM tray.
I noticed that Andreas Magnusson's answer didn't quite work exactly the same as Explorer's 'Eject' button did. Specifically, the drive wasn't grayed out in Explorer using Andreas' code, but was if you used the Eject command. So I did some investigating.
I ran API Monitor while running the Eject command from Explorer (Windows 7 SP1 64-bit). I also found a good (now-defunct) MSKB article 165721 titled How To Ejecting Removable Media in Windows NT/Windows 2000/Windows XP. The most interesting part of the article is quoted below:
- Call CreateFile with GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, and OPEN_EXISTING. The lpFileName parameter should be \\.\X: (where X is the real drive letter). All other parameters can be zero.
- Lock the volume by issuing the FSCTL_LOCK_VOLUME IOCTL via DeviceIoControl. If any other application or the system is using the volume, this IOCTL fails. Once this function returns successfully, the application is guaranteed that the volume is not used by anything else in the system.
- Dismount the volume by issuing the FSCTL_DISMOUNT_VOLUME IOCTL. This causes the file system to remove all knowledge of the volume and to discard any internal information that it keeps regarding the volume.
- Make sure the media can be removed by issuing the IOCTL_STORAGE_MEDIA_REMOVAL IOCTL. Set the PreventMediaRemoval member of the PREVENT_MEDIA_REMOVAL structure to FALSE before calling this IOCTL. This stops the device from preventing the removal of the media.
- Eject the media with the IOCTL_STORAGE_EJECT_MEDIA IOCTL. If the device doesn't allow automatic ejection, then IOCTL_STORAGE_EJECT_MEDIA can be skipped and the user can be instructed to remove the media.
- Close the volume handle obtained in the first step or issue the FSCTL_UNLOCK_VOLUME IOCTL. This allows the drive to be used by other processes.
Andreas's answer, the MSKB article, and my API sniffing of Explorer can be summarized as follows:
CreateFile
called to open the volume. (All methods).DeviceIoControl
called with FSCTL_LOCK_VOLUME
. (All methods).DeviceIoControl
called with FSCTL_DISMOUNT_VOLUME
. (Andreas's and MSKB methods only. Explorer does not call this for some reason. This IOCTL seems to be what affects whether the drive is grayed out in Explorer or not. I am not sure why Explorer doesn't call this).DeviceIoControl
called with IOCTL_STORAGE_MEDIA_REMOVAL
and PREVENT_MEDIA_REMOVAL
member set to FALSE
(MSKB and Explorer methods. This step is missing from Andreas's answer).DeviceIoControl
called with IOCTL_STORAGE_EJECT_MEDIA
(Andreas and MSKB article) or IOCTL_DISK_EJECT_MEDIA
(Explorer; note this IOCTL was obsoleted and replaced with the STORAGE IOCTL. Not sure why Explorer still uses the old one).To conclude, I decided to follow the procedure outlined in the MSKB article, as it seemed to be the most thorough and complete procedure, backed up with an MSKB article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With