Ejecting or closing the CDROM drive (specifying a drive letter)
   


To open or close the CDROM, call OpenCloseCDDrive() passing one of the TCDAction types. This function will work on all types of CDs and will also work if there is no CDROM in the drive. This is similar to OpenCloseCD() except that this one requires a drive letter.

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;



Blue Orb Software

[email protected]