Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Open CD Tray

6 views
Skip to first unread message

Kevin Morris

unread,
Jul 24, 2001, 5:10:10 PM7/24/01
to
Is there a piece of code I can use to open the CD tray?
Thanks in advance.

Jon Scott

unread,
Jul 24, 2001, 5:52:21 PM7/24/01
to
Here are a couple of ways:

1)

uses MMSystem, MPlayer;

type
TCDAction = (_OPEN, _CLOSE);

function OpenCloseCDDrive(Drive: Char; Action: TCDAction): boolean;
{ func to eject or close the cdrom drive. Works on all Audio & }
{ Data CDs, even if there is no CDROM in the drive. }
var
mp : TMediaPlayer;
mciResult: integer;
begin
Result := False;
Application.ProcessMessages;
if not IsDriveCD(Drive) then
begin
MessageDlg(Drive + ':\ is not a CDROM drive.', mtError, [mbOK], 0);
Exit;
end;
Screen.Cursor := crHourGlass;
mp := TMediaPlayer.Create(nil);
try
mp.Visible := False;
mp.Parent := Application.MainForm;
mp.Shareable := True;
mp.DeviceType := dtCDAudio;
mp.FileName := Drive + ':';
mp.Open;
Application.ProcessMessages;
mciResult := 0;
case Action of
_OPEN : mp.Eject;
_CLOSE: mciResult := mciSendCommand(mp.DeviceID,
MCI_SET, MCI_SET_DOOR_CLOSED, 0);
end;
Application.ProcessMessages;
mp.Close;
Application.ProcessMessages;
case Action of
_OPEN : Result := True;
_CLOSE: Result := mciResult = 0;
end;
finally
mp.Free;
Screen.Cursor := crDefault;
end;
end;

and 2)

uses MMSystem;

procedure OpenCloseCD(TrueForOpenFalseForClose: boolean);
{ Works as well as OpenCloseCDDrive() above, }
{ but you don't have to specify a drive letter. }
{ Thanks to Bence Parhuzamos [parhu...@yahoo.com] for this code. }
var
mci: TMCI_Open_Parms;
begin
FillChar(mci, SizeOf(mci), #0);
mci.lpstrDeviceType := PChar('CDAudio');
mciSendCommand(0, mci_Open, mci_Open_Type, Longint(@mci));
mciSendCommand(mci.wDeviceID, mci_Set, 256*(Byte(not TrueForOpenFalseForClose)+1), 0);
{ MCI_SET_DOOR_OPEN = 256 }
{ MCI_SET_DOOR_CLOSED = 512 }
mciSendCommand(mci.wDeviceID, mci_Close, 0, 0);
end;

--
Thanks,
Jon.


"Kevin Morris" <morr...@spar.ca> wrote in message news:3b5de3f4...@newsgroups.borland.com...

Jon Scott

unread,
Jul 24, 2001, 5:54:36 PM7/24/01
to
Oops. You're probably wondering what the IsDriveCD() function looks like:

function IsDriveCD(Drive: Char): boolean;
{ func to determine if a given drive is a CDROM drive. }
var
DrivePath: string;
DriveResult: integer;
begin
DrivePath := Drive + ':\';
DriveResult := GetDriveType(PChar(DrivePath));
Result := DriveResult = DRIVE_CDROM;
end;

--
Thanks,
Jon.


"Jon Scott" <jes...@email.com> wrote in message news:3b5dee7f_1@dnews...

Jack Sudarev

unread,
Jul 24, 2001, 7:55:08 PM7/24/01
to
Kevin Morris <morr...@spar.ca> wrote in message
news:3b5de3f4...@newsgroups.borland.com...
> Is there a piece of code I can use to open the CD tray?
> Thanks in advance.

sure
uses MMSystem;

..
MciSendString('set cdaudio door open', nil, 0, 0);
..

Regards,

Jack Sudarev.

0 new messages