Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows CDROM Eject

Tags:

windows

winapi

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.

like image 398
kenny Avatar asked Sep 12 '08 10:09

kenny


People also ask

How do you eject a CD from Windows 10?

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.

Where is the CD eject button?

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.

Why is my CD not ejecting?

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.


1 Answers

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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:

  1. CreateFile called to open the volume. (All methods).
  2. DeviceIoControl called with FSCTL_LOCK_VOLUME. (All methods).
  3. 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).
  4. 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).
  5. 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.

like image 90
James Johnston Avatar answered Nov 02 '22 04:11

James Johnston