Open & Close Optical Disk Drive

From NSIS Wiki

I sorted out the WinAPI Calls for open and close CD/DVD Drives. Note that if you have more than one drive installed, these calls will affect only the primary one.

The Basics

By researching on the web, I found that you need to use the WinAPI function mciSendString to send a so called command string to the drive. It's implemented as ANSI version from on Win95 (mciSendStringA) and as Unicode version from Win2000, XP and above (mciSendStringU). It's located in winmm.dll in the system32 directory.

From MSDN we get the right syntax definition:

MCIERROR mciSendString(
 LPCTSTR lpszCommand, // Pointer to a null-terminated string that specifies an MCI command string.
 LPTSTR lpszReturnString, // Pointer to a buffer that receives return information.
 UINT cchReturn, // Size, in characters, of the return buffer specified by the lpszReturnString parameter.
 HANDLE hwndCallback // Handle to a callback window if the "notify" flag was specified in the command string.
);

So lets translate it to a NSIS System.dll Plugin call:

System::Call "winmm::mciSendStringA(t 'command string', t .s, i ${NSIS_MAX_STRLEN},i $HWNDPARENT)i .s"

Note, that here I used the stack for any ouput (.s). I also used the ANSI version as you normally don't need to use Unicode anyway. As buffer size I recommend to always use ${NSIS_MAX_STRLEN} for any call you use, except if you want a smaller string for some reason. As callback handle of course set the handle of the installer ($HWNDPARENT).

For the command string having his own complex syntax, I sorted the two interesting ones out:

Open the Drive

System::Call "winmm::mciSendStringA(t 'set cdaudio door open', t .r0, i ${NSIS_MAX_STRLEN},i $HWNDPARENT)i .r1"

Close the Drive

System::Call "winmm::mciSendStringA(t 'set cdaudio door close', t .r0, i ${NSIS_MAX_STRLEN},i $HWNDPARENT)i .r1"

Output and Return

Note that you may not want to get any output. You then just can use t .n instead of t .r0 (or any other variable). Same for any other output. See the System Plugin Readme for more information about interaction with NSIS variables.

Further Reading

Personal tools
donate