Question
How can you use Delphi to open and close the CD-Rom drive?
ANSWER:
Start Delphi and select File | New | Application. In Unit1.pas add MMSystem to the uses. Drop two buttons on your form. Name the first button btnOpen and name the second button btnClose. Change the caption of btnOpen to Open and the caption of btnClose to Close. Add the following code to the btnOpen OnClick event:
procedure TForm1.btnOpenClick(Sender: TObject);
begin
mciSendString('Set cdaudio door open wait', nil, 0, 0);
end;
Now add the following code to the btnClose OnClick event:
procedure TForm1.btnCloseClick(Sender: TObject);
begin
mciSendString('Set cdaudio door closed wait', nil, 0, 0);
end;
The code I just demonstrated will open up the default CD-Rom drive. What if you have a CD-Rom drive along with a CD-Burner? Here is an example of how you would do that. In the btnOpen OnClick event, place the following code:
procedure TForm1.btnOpenClick(Sender: TObject);
begin
// Open drive X: (X being the letter of the drive you wish to open)
mciSendString('open cdaudio!X: alias driveX', nil, 0, 0);
mciSendString('set driveX door open wait', nil, 0, 0);
end;
Now go ahead and add the following to the btnClose OnClick Event:
procedure TForm1.btnCloseClick(Sender: TObject);
begin
// Close drive X:
mciSendString('set driveX door closed wait', nil, 0, 0);
end;