Ze CD-ROM door opener/closer

vegaseat 1 Tallied Votes 638 Views Share

A real useful piece of code, hence I put it under Z. It does nothing but open and close the door of the CD-ROM player, and pushes the tray in and out. Should your coffee be too hot, you can put it on the moving tray and cool it off! Get the old computer out of the attic, we found a use for it! Seriously, just a quick look at the many things the mciSendString() function can do.

// use mciSendString() to open and close the CD-ROM door
// you have to link with the winmm.lib file, or
// in the case of Dev-C++ link with libwinmm.a via
// Project>>Project Options>>Parameters>>Add Lib>>libwinmm.a
// a Dev-C++ tested console application by  vegaseat  18dec2004

#include <iostream>  
#include <windows.h>   // Sleep() 
#include <mmsystem.h>  // mciSendString()

using namespace std;   // std::cout, std::cin

int main()  
{  
  mciSendString("open CDAudio", NULL, 0, NULL);
  Beep(440,1000);   
  cout << "Opening CD-ROM door ..." << endl;  
  mciSendString("set CDAudio door open", NULL, 0, NULL);  

  cout << "Closing the CD-ROM door in 5 seconds ..." << endl;  
  Sleep(5000);  
  mciSendString("set CDAudio door closed", NULL, 0, NULL);  

  mciSendString("close CDAudio", NULL, 0, NULL);

  cin.get();  // wait  
  return 0;  
}
inparanoia 0 Newbie Poster

Sometimes i can't open that door also with phisical button!!!(i've found how to open it with a metal bar)

venomlash 55 Junior Poster

:!: Daaaaaaaaang! That's pretty cool!
But good thing it closes it too. I have a friend who twice had the power cut out while the CD tray was open, and he naturally stumbled into the open tray, ripping it out of the computer--both times!!! Bad luck, huh?

Member Avatar for Dukane
Dukane

That's pretty cool! I wish you'd provide an explanation on how it works.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Not much to be explained here except for the thing that this code basically relies on the function mciSendString( ) . A reference to that can be found here.

Here MCI stands for Media control Interface and mmsystem.h stands for MultiMedia System Header file. BTW these two terms are specific to MS Windows only.

banders7 1 Newbie Poster

If you wish to open a specific drive tray ....
// Must open "shareable" to avoid "driver already in use error"
// "CDROM" can be any word you choose to be an alias
sprintf(openstring,"open %c: type cdaudio alias CDROM shareable",Drive);
mciSendString(openstring,NULL,0,0);
mciSendString("set CDROM door open wait",NULL,0,0);
mciSendString("set CDROM door closed wait",NULL,0,0),
mciSendString("close CDROM",NULL,0,0);

Loke98 0 Newbie Poster

[Linker error] undefined reference to `mciSendStringA@16'
This is the error I get when I try to run the program, I use devcpp as compiler.

Shft 0 Light Poster

May I ask, which library did you include to make this program?

RonalBertogi 46 Junior Poster in Training

include winmm.lib in your project imports or do the pragma: #pragma comment(lib, "winmm.lib")

BTW, here is my own version. Additional to this is you can lock/unlock the drive.

// dischander.h

#pragma once

struct DiscHandler
{
    // I assume these routines are self-explanatory
    static BOOL Eject(BYTE bDrive);
    static BOOL Load(BYTE bDrive);
    static BOOL IsReady(BYTE bDrive);
    static DWORD GetSerial(BYTE bDrive, wchar_t * pLabelBuf = 0, int chBuf = 0);
    static void Lock(BYTE bDrive, BOOL fLock);

private:
    struct DeviceHandle
    {
        DeviceHandle(BYTE bDrive);
        ~DeviceHandle();
        operator HANDLE();
        HANDLE m_handle;
    };
};

#define EjectDrive     DiscHandler::Eject
#define LoadDrive      DiscHandler::Load
#define IsDriveReady   DiscHandler::IsReady
#define GetDiscSerial  DiscHandler::GetSerial
#define LockDrive      DiscHandler::Lock

...and the definition

#include "dischandler.h"
#include <winioctl.h>

DiscHandler::DeviceHandle::DeviceHandle(BYTE bDrive)
{
    wchar_t szRoot[] = L"\\\\.\\?:";
    szRoot[4] = bDrive;
    m_handle = CreateFile(szRoot, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
        OPEN_EXISTING, 0, NULL);
    if( m_handle==INVALID_HANDLE_VALUE )
        m_handle = NULL;
}

DiscHandler::DeviceHandle::~DeviceHandle()
{
    if( m_handle )
        CloseHandle(m_handle);
}

DiscHandler::DeviceHandle::operator HANDLE()
{
    return m_handle;
}

BOOL DiscHandler::Eject(BYTE bDrive)
{
    DeviceHandle hDrive = bDrive;
    if( hDrive )
    {
        DWORD dwBytesReturned;
        return DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
    }
    return FALSE;
}

BOOL DiscHandler::Load(BYTE bDrive)
{
    DeviceHandle hDrive = bDrive;
    if( hDrive )
    {
        DWORD dwBytesReturned;
        return DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
    }
    return 0;
}

BOOL DiscHandler::IsReady(BYTE bDrive)
{
    DeviceHandle hDrive = bDrive;
    if( hDrive )
    {
        DWORD dwBytesReturned;
        return DeviceIoControl(hDrive, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
    }
    return 0;
}

DWORD DiscHandler::GetSerial(BYTE bDrive, wchar_t * pLabelBuf, int chBuf)
{
    DWORD dwSerial = 0;
    wchar_t wsz[] = L"?:\\";
    *wsz = bDrive;
    GetVolumeInformation(wsz, pLabelBuf, chBuf, &dwSerial, NULL, NULL, NULL, 0);
    return dwSerial;
}

void DiscHandler::Lock(BYTE bDrive, BOOL fLock)
{
    DeviceHandle hDrive = bDrive;
    DWORD dwBytesReturned;
    PREVENT_MEDIA_REMOVAL pmr;
    pmr.PreventMediaRemoval = fLock;
    DeviceIoControl(hDrive, IOCTL_STORAGE_MEDIA_REMOVAL, &pmr, sizeof(PREVENT_MEDIA_REMOVAL),
        NULL, 0, &dwBytesReturned, NULL);
}

Anyone can use this code if they think it has some benefits. You can modify them if you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.