<p>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. </p>

<p>I am especially looking for a method to do this from a batch file, if possible, but API calls would be OK.</p>

[{'answer_id': 58678, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.nirsoft.net/utils/nircmd.html" rel="nofollow noreferrer">Nircmd</a> is a very handy freeware command line utility with various options, including opening and closing the CD tray.</p>\n'}, {'answer_id': 58725, 'author': 'DaveK', 'author_id': 4244, 'author_profile': 'https://Stackoverflow.com/users/4244', 'pm_score': 4, 'selected': True, 'text': '<p>Here is an easy way using the Win32 API:</p>\n\n<pre><code>\n[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]\n        protected static extern int mciSendString(string lpstrCommand,StringBuilder lpstrReturnString,int uReturnLength,IntPtr hwndCallback);\n\n public void OpenCloseCD(bool Open)\n {\n    if (Open)\n    {\n        mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);\n    }\n    else\n    {\n        mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);\n    }\n}\n\n</code></pre>\n'}, {'answer_id': 77291, 'author': 'Andreas Magnusson', 'author_id': 5811, 'author_profile': 'https://Stackoverflow.com/users/5811', 'pm_score': 4, 'selected': False, 'text': '<p>I kind of like to use DeviceIOControl as it gives me the possibility to eject any kind of removable drive (such as USB and flash-disks as well as CD trays). Da codez to properly eject a disk using DeviceIOControl is (just add proper error-handling):</p>\n\n<pre><code>bool ejectDisk(TCHAR driveLetter)\n{\n  TCHAR tmp[10];\n  _stprintf(tmp, _T("\\\\\\\\.\\\\%c:"), driveLetter);\n  HANDLE handle = CreateFile(tmp, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);\n  DWORD bytes = 0;\n  DeviceIoControl(handle, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &amp;bytes, 0);\n  DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0, &amp;bytes, 0);\n  DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, &amp;bytes, 0);\n  CloseHandle(handle);\n  return true;\n}\n</code></pre>\n'}, {'answer_id': 30512979, 'author': 'James Johnston', 'author_id': 562766, 'author_profile': 'https://Stackoverflow.com/users/562766', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n<p>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 <a href="http://web.archive.org/web/20150511141059/https://support.microsoft.com/en-us/kb/165721" rel="nofollow noreferrer" title="How To Ejecting Removable Media in Windows NT/Windows 2000/Windows XP">How To Ejecting Removable Media in Windows NT/Windows 2000/Windows XP</a>. The most interesting part of the article is quoted below:</p>\n<blockquote>\n<ol>\n<li>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.</li>\n<li>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.</li>\n<li>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.</li>\n<li>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.</li>\n<li>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.</li>\n<li>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\nprocesses.</li>\n</ol>\n</blockquote>\n<p>Andreas\'s answer, the MSKB article, and my API sniffing of Explorer can be summarized as follows:</p>\n<ol>\n<li><code>CreateFile</code> called to open the volume. (All methods).</li>\n<li><code>DeviceIoControl</code> called with <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364575%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>FSCTL_LOCK_VOLUME</code></a>. (All methods).</li>\n<li><code>DeviceIoControl</code> called with <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364562%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>FSCTL_DISMOUNT_VOLUME</code></a>. (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).</li>\n<li><code>DeviceIoControl</code> called with <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363416(v=vs.85).aspx" rel="nofollow noreferrer"><code>IOCTL_STORAGE_MEDIA_REMOVAL</code></a> and <code>PREVENT_MEDIA_REMOVAL</code> member set to <code>FALSE</code> (MSKB and Explorer methods.  This step is missing from Andreas\'s answer).</li>\n<li><code>DeviceIoControl</code> called with <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363406(v=vs.85).aspx" rel="nofollow noreferrer"><code>IOCTL_STORAGE_EJECT_MEDIA</code></a> (Andreas and MSKB article) or <code>IOCTL_DISK_EJECT_MEDIA</code> (Explorer; note this IOCTL was obsoleted and replaced with the STORAGE IOCTL. Not sure why Explorer still uses the old one).</li>\n</ol>\n<p>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.</p>\n'}, {'answer_id': 32371540, 'author': 'Slion', 'author_id': 3969362, 'author_profile': 'https://Stackoverflow.com/users/3969362', 'pm_score': 1, 'selected': False, 'text': '<p>To close the drive tray do as described <a href="https://stackoverflow.com/a/30512979/3969362">here</a> but instead of using DeviceIoControl with IOCTL_STORAGE_EJECT_MEDIA you need to call DeviceIoControl with IOCTL_STORAGE_LOAD_MEDIA.</p>\n'}]
['https://Stackoverflow.com/questions/58670', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3225/']