11

I've always wondered why Windows has an Eject option but doesn't include a corresponding Close / Close Tray option in the context menu of optical drives.

Is there a way of closing the optical drive tray without the use of any third party software in Windows?

6
  • 1
    Did you want this option in the case you are remotely accessing a computer? Dec 6, 2012 at 5:58
  • 5
    @ChristopherChipps Is he going to remotely put the CD in the tray as well?
    – cutrightjm
    Dec 6, 2012 at 6:02
  • 1
    Touche @ekaj. Sure, seriously in the case the user needs the tray opened and is unable to themself. In which case this is needed, I am unsure. Dec 6, 2012 at 6:07
  • 2
    Remember that (most, if not all) laptop optical drives have to be pushed to be closed, since the tray cannot retract automatically. I suppose they could have somehow made the drive advertise whether it supports automatic tray retraction or not, and the OS could have displayed or hidden the Close option based on this, but really, that would just be way too much time and resources spent on something so trivial and mostly useless.
    – Karan
    Dec 8, 2012 at 2:43
  • 2
    +1 for the question. @ekaj, while it is unusual to need to remotely close a disc tray, there are times when such functionality is a convenience. I remotely authored a CD for a client and wanted to check that the burn was successful, however the authoring software auto-ejected the disk. At home, if I'm not satisfied with the encoding of video rip on a linux server, I can type eject -t from my laptop and re-encode without having to walk to the server closet to close the tray manually. A rather small convenience, but perhaps not as absurd as the down votes suggest.
    – jthetzel
    Dec 8, 2013 at 14:08

9 Answers 9

15

The only way to do this IMO without the use of 3rd party utils (such as NirCmd and Wizmo) would be via VBScript or PowerShell. All the VBScript solutions I've seen so far use an outdated Windows Media Player OCX. I don't know whether the latest versions of WMP include an OCX with similar functionality or not, plus disabling/uninstalling it via Windows Features might interfere with the functioning of the script in any case.

A common way to implement this functionality via code is by using the Media Control Interface (MCI) APIs (specifically, the set command). However, since VBScript does not support calling normal Windows API functions or even functions from arbitrary DLLs, that leaves us with PowerShell. Thus the following should work out of the box in Windows 7+ that comes with PS pre-installed, and on XP/Vista after PS is installed. The MCI DLL i.e. Windows\System32\WinMM.dll should be available as part of the default installation in XP+.

1) Save the following as CD_Open.ps1:

$cd = Add-Type -memberDefinition @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
"@ -passthru -name mciSendString
$cd::mciSendStringA('set cdaudio door open', $null, 0, 0);

2) Save the following as CD_Close.ps1:

$cd = Add-Type -memberDefinition @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
"@ -passthru -name mciSendString
$cd::mciSendStringA("set cdaudio door closed", $null, 0, 0);

Now comes the problem. By default, unsigned PS scripts cannot be executed in Windows for security reasons. Type get-help about_signing at the PS prompt to know more about this, including how to self sign your scripts and so on.

Luckily there's a workaround as the get-help command above states:

TO PERMIT SIGNED SCRIPTS TO RUN
-------------------------------
   When you start Windows PowerShell on a computer for the first time, the
   Restricted execution policy (the default) is likely to be in effect.

   The Restricted policy does not permit any scripts to run.

   To find the effective execution policy on your computer, type:

       get-executionpolicy

   To run unsigned scripts that you write on your local computer and signed
   scripts from other users, use the following command to change the execution
   policy on the computer to RemoteSigned:

       set-executionpolicy remotesigned

   For more information, see Set-ExecutionPolicy.

3) So from an elevated Command Prompt, run the following command:

powershell set-executionpolicy remotesigned

(You can run powershell set-executionpolicy restricted to revert to the default setting.)

This command needs to be run only once and remains in effect until you change the execution policy again.

4) Now you can use the following commands (even from a non-elevated Command Prompt) to open/close the optical drive tray:

powershell -file CD_Open.ps1
powershell -file CD_Close.ps1

Of course, you can create shortcuts as well so that you can open/close the tray with a click or a key combo:

CD Tray Shortcuts

You can also add the Close command to the context menu of your optical drive using the following .REG file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell]
@="none"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\closetray]
@="Close"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\closetray\command]
@="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Powershell.exe -windowstyle hidden -file I:\\CD_Close.ps1"

(Edit the paths as required. Also, the -WindowStyle parameter is only available with PS 2.0+.)

2
  • 2
    I would +1 you for the PowerShell script, and then +1 again for the Registry idea.
    – palswim
    Sep 15, 2017 at 19:37
  • With one of the updates, Windows 10 removed my Drive.CDROM key from the registry. I re-added it to the SystemFileAssociations key and this solution still works.
    – palswim
    Sep 26, 2018 at 0:28
4

Here is an example. Here is an implementation in C#.. (may require a DLL or two).

There are lots of them, so if one doesn't work for you try another.

1
  • Well, I did search. But couldn't find other than downloading something . The command ling thing is useful, thanks.
    – Reza M.
    Dec 6, 2012 at 5:35
2

I haven't tried these myself, but may be this will help Link
But you have to download a dll
Here is the C source code to do it if you want to compile it yourself, but you need to look for a way to integrate the compiled program in the Windows Explorer right click menu for the CD drive C code

1
  • The CDR program you link to includes an executable in addition to the C source code. From the command line, CDR close D: solved the problem of needing to remotely close a disc tray.
    – jthetzel
    Dec 8, 2013 at 14:28
2

there is another way. in Windows Media Player (at least in 12), you can click, at the top, Play, then Eject, and choose your drive. if it is already ejected, nothing will happen. then do it again, and it should CLOSE the drive :)

2

I followed Karan's answer and it worked!

The only thing I wanted to improve was to hide the console window that flashes when running the context menu or shortcut. I saved the return value to a variable and compiled the powershell scripts to gui executables:

  1. Save the following as tray_open.ps1:
$cd = Add-Type -memberDefinition @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
"@ -passthru -name mciSendString
$result = $cd::mciSendStringA('set cdaudio door open', $null, 0, 0);
  1. Save the following as tray_close.ps1:
$cd = Add-Type -memberDefinition @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
"@ -passthru -name mciSendString
$result = $cd::mciSendStringA("set cdaudio door closed", $null, 0, 0);
  1. Download Microsoft's WMF 5.1 if the OS is not Windows 10 or later:
powershell.exe
Set-ExecutionPolicy RemoteSigned
.\Install-WMF5.1.ps1
<click> "Restart Now"
  1. Install the applicable packages and modules:
powershell -version 3.0
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name ps2exe -RequiredVersion 1.0.5
  1. Compile the powershell scripts into gui executables tray_open.exe and tray_close.exe:
powershell.exe
ps2exe tray_open.ps1 -noConsole
ps2exe tray_close.ps1 -noConsole
  1. Lastly, save the following commands into a tray_cmd.reg file and then run it to create cdrom context menus (change exe pathnames as needed for your system):
REGEDIT4

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell]
@="none"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\closetray]
@="Tray Close"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\closetray\command]
@="I:\\utils\\scripts\\exe\\tray_close.exe"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\opentray]
@="Tray Open"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\opentray\command]
@="I:\\utils\\scripts\\exe\\tray_open.exe"
0

It could be that the Eject button was originally designed for cases of hardware failure where the hardware button did not respond any more.

Close on the other hand can typically be done by just pushing the drive in. This maybe the reason for not adding a close.

Also eject works well on virtual/mounted drives(mac dmgs, any mounted drives), usb drives (as safely remove) etc, where there is no version of close.

1
  • Huh, good point for the hardware failure part. Wouldn't of thought about that. I could see that being true as a lot of cases where the original design/goal doesn't apply any more but they still kept it.
    – Reza M.
    Dec 6, 2012 at 5:31
0

In Linux (and therefore, probably in OSX and BSD and UNIXes etc etc), "there's an app for that":

eject -t
0

You did not specify which version of Windows you are running. I just noticed on my Windows 2000 system, the CD Player program (under Start > Accessories > Entertainment) has an eject button. Clicking on that Eject button will open the tray if the tray is closed, and it will close the tray if the tray is open.

0

This is a small app that adds a button to the task bar for opening and closing the optical drive. http://digola.com/doorcontrol.html

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .