Link to home
Start Free TrialLog in
Avatar of Dennis9
Dennis9

asked on

Eject CD Rom??

Hi Experts.
I want to be able to open/close cdrom drive, via one button. That means i need to know if the cdrom is currently open or closed.

How can i get the status of the drive (open or closed)?

i tried this code:
if MediaPlayer1.Mode = mpOpen then
  showmessage('CDTrayOpen')
else
  showmessage('CDTrayClosed');

But it always return false.

Also note, if its possible to get som code that get the status of the cdrom drive right, even if my program was not started when cd was ejected.

Thanks
Avatar of waltham
waltham

Please try below code


function TForm1.IsDriveCD(Drive : char) : longbool;
var
  DrivePath : string;
begin
  DrivePath := Drive + ':\';
  result := LongBool(GetDriveType(PChar(DrivePath)) and DRIVE_CDROM);
end;

function TForm1.EjectCD(Drive : char) : bool;
var
  mp : TMediaPlayer;
begin
  result := false;
  Application.ProcessMessages;
  if not IsDriveCD(Drive) then exit;
  mp := TMediaPlayer.Create(nil);
  mp.Visible := false;
  mp.Parent := Application.MainForm;
  mp.Shareable := true;
  mp.DeviceType := dtCDAudio;
  mp.FileName := Drive + ':';
  mp.Open;
  Application.ProcessMessages;
  mp.Eject;
  Application.ProcessMessages;
  mp.Close;
  Application.ProcessMessages;
  mp.free;
  result := true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not EjectCD('D') then
    ShowMessage('Not A CD Drive');
end;
Avatar of Dennis9

ASKER

waltham -> Your code just opens the drive. I wanna know if the drive currently is open or if it is closed.
i know you are a delphi programmer but i think the information in this link will help a lot ( my vb group use to use this information )

http://www.mvps.org/vbnet/index.html?code/toc/tocbyuse.htm

Try this (Unsure if works):

procedure TForm1.Button1Click(Sender: TObject);
var
  S : array[0..64] of Char;
  Error : Cardinal;
  Text : array[0..255] of Char;
begin
  Error := mciSendString('open cdaudio alias cd', nil, 0, Handle);
  if Error <> 0 then
  begin
    mciGetErrorString(Error, @Text, 255);
    ShowMessage(Text);
    mciSendString('close cd', nil, 0, Handle);
    Exit;
  end;
  Error := mciSendString('status cd mode', @S, SizeOf(S), Handle);
  if Error <> 0 then
  begin
    mciGetErrorString(Error, @Text, 255);
    ShowMessage(Text);
    mciSendString('close cd', nil, 0, Handle);
    Exit;
  end;
  mciSendString('close cd', nil, 0, Handle);
  ShowMessage(S);
end;

Cheers,

Alan
Agree with alanwhincup . Just add mmsystem at uses.
Avatar of Dennis9

ASKER

alanwhincup -> Your code would work if not mciSendString always did resturn 0 :<

Its hard when the MMSystem has errors. It always return 0, probaly because MMSystem think it had a success when it resive the command.

Maybe this question is impossible to solve?

Dennis
there is no way to know if the tray is
Opened or Closed... I remember on of the
big experts here once ask for it offering
2000 points and no answer..
Well,
i disagree bryan7, btw)) been a long time ;-)
everything can be done, even if hard-coded but it can be done !!

don't you think so !!

regards,
</Ruslan>
umm, do it so...
you will get exactly same results Drive not ready
with tray open or close (with no cd)
there is no flag or anything to indicate
if the tray is open.
How About This Snippet ??
I Want To Make This A Bet bryan7 !! ;-)
Just For Interest,


//*
Uniform CDROM driver ioctls

CDROMCLOSETRAY pendant of CDROMEJECT
CDROM_SET_OPTIONS Set behavior options
CDROM_CLEAR_OPTIONS Clear behavior options
CDROM_SELECT_SPEED Set the CD-ROM speed
CDROM_SELECT_DISC Select disc (for juke-boxes)
CDROM_MEDIA_CHANGED Check is media changed
CDROM_DRIVE_STATUS Get tray position, etc.
CDROM_DISC_STATUS Get disc type, etc.
CDROM_CHANGER_NSLOTS Get number of slots
CDROM_LOCKDOOR lock or unlock door
CDROM_DEBUG Turn debug messages on/off
CDROM_GET_CAPABILITY get capabilities
DVD-ROM Specific ioctls

DVD_READ_STRUCT Read structure
DVD_WRITE_STRUCT Write structure
DVD_AUTH Authentication
writing cdroms

CDROM_SEND_PACKET send a packet to the drive
CDROM_NEXT_WRITABLE get next writable block
CDROM_LAST_WRITTEN get last block written on disc
capability flags used with the uniform CD-ROM driver

  CDC_CLOSE_TRAY          0x1     /* caddy systems _can't_ close */
  CDC_OPEN_TRAY           0x2     /* but _can_ eject.  */
  CDC_LOCK                0x4     /* disable manual eject */
  CDC_SELECT_SPEED        0x8     /* programmable speed */
  CDC_SELECT_DISC         0x10    /* select disc from juke-box */
  CDC_MULTI_SESSION       0x20    /* read sessions>1 */
  CDC_MCN                 0x40    /* Medium Catalog Number */
  CDC_MEDIA_CHANGED       0x80    /* media changed */
  CDC_PLAY_AUDIO          0x100   /* audio functions */
  CDC_RESET               0x200   /* hard reset device */
  CDC_IOCTLS              0x400   /* driver has non-standard ioctls */
  CDC_DRIVE_STATUS        0x800   /* driver implements drive status */
  CDC_GENERIC_PACKET      0x1000  /* driver implements generic packets */
  CDC_CD_R                0x2000  /* drive is a CD-R */
  CDC_CD_RW               0x4000  /* drive is a CD-RW */
  CDC_DVD                 0x8000  /* drive is a DVD */
  CDC_DVD_R               0x10000 /* drive can write DVD-R */
  CDC_DVD_RAM             0x20000 /* drive can write DVD-RAM */

drive status possibilities returned by CDROM_DRIVE_STATUS ioctl

CDS_NO_INFO if not implemented
CDS_NO_DISC
CDS_TRAY_OPEN
CDS_DRIVE_NOT_READY
CDS_DISC_OK
return values for the CDROM_DISC_STATUS ioctl can also return CDS_NO_[INFO|DISC], from above

CDS_AUDIO
CDS_DATA_1
CDS_DATA_2
CDS_XA_2_1
CDS_XA_2_2
CDS_MIXED
User-configurable behavior options for the uniform CD-ROM driver

CDO_AUTO_CLOSE close tray on first open()
CDO_AUTO_EJECT open tray on last release()
CDO_USE_FFLAGS use O_NONBLOCK information on open
CDO_LOCK lock tray on open files
CDO_CHECK_TYPE check type on open for data


*//


i wish i hgad the enough capabilities to translate C into DELPHI, i would have posted the needed code !!!

IF YOU CAN I CAN EMAIL YOU THE c++ CODE, I THINK I STILL HAVE YOUR EMAIL !!!

REGARDS,
</rUSLAN>
You Will Need To Notice This From The Mess I Just Posted..



drive status possibilities returned by CDROM_DRIVE_STATUS ioctl

CDS_NO_INFO if not implemented
CDS_NO_DISC
CDS_TRAY_OPEN
CDS_DRIVE_NOT_READY
CDS_DISC_OK
return values for the CDROM_DISC_STATUS ioctl can also return CDS_NO_[INFO|DISC], from above





Regards again,
</Ruslan>
Great, Just Great...

Let Me Be Some Kinda Selfish, ;-p

for some more points am gonna post the Delphi Unit, i have it ready !!

regards,
</Ruslan>
Avatar of Dennis9

ASKER

ok 100 plint is that enough?
ASKER CERTIFIED SOLUTION
Avatar of neostudio
neostudio

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On More Great And Nice Code Is Available At This Address, It Also Provide Some Flags For The CD-ROM DOOR,

[Open-Close]

Here We Go...


http://home.student.utwente.nl/h.vanwelbergen/code/pascal/cd.html



regards,
</Ruslan>
ummmm... wow, can't wait to test that..
can I have the C files ? or maybe the
translated one ? can't wait to test it..
i know you are a delphi programmer but i think the information in this link will help a lot ( my vb group use to use this information )

http://www.mvps.org/vbnet/index.html?code/toc/tocbyuse.htm

interesting
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from neostudio

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
Thanks In Advance pnh73






==========================================
Ruslan K. Abu Zant
phpX(c) CEO
http://www.phpx.info/
PHP Xperts Community
public@phpx.info
==========================================
Learn How To Ask Questions And Open Discussion Threads
http://catb.org/~esr/faqs/smart-questions.html
==========================================