We help IT Professionals succeed at work.

How to deal with cdrom\dvd drives and inserted cdroms\dvd properly?

PeterdeB
PeterdeB asked
on
Medium Priority
362 Views
Last Modified: 2010-04-05
Hi my friends!

My program should search for a file located on a cdrom inserted in some drive being either a cdrom or dvd drive. It would be nice if the program popped up a messagebox when it was done searching, stating that it had found the file + displaying the path to the file or stating it had not found the file because no cd was inserted, the cd could not be read or no cdrom dvd drive was present.

Kind regards,

Paul :)

Ps working samples do the trick. Please comment if you use complex code tnx in advance!
Comment
Watch Question

Author

Commented:
Something like this seems to work >

procedure TForm1.btnCBDClick(Sender: TObject);
var i, ErrorMode: integer;
    Drive: String;
    C: Array[0..10] of Char;
    FatType: array [0..MAX_PATH] of Char;
    MaxLen1,fSysFlags: DWORD;
begin
   For i := 1 to 26 do begin
      StrPCopy(C, Chr(I+64)+':\');
      If GetDriveType(C) =  DRIVE_CDROM then
       begin
        FatType[0] := #0;
        Drive := C;
        ErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
if not GetVolumeInformation(PChar(Drive), nil, 0,nil, MaxLen1, fSysFlags,FatType,DWORD(sizeof(FatType))) then
  begin
  ShowMessage('In station:  '+ Drive + ' is geen cdrom gevonden!');
  end else
  PageControl1.ActivePageIndex:=PageControl1.ActivePageIndex +1;
SetErrorMode(ErrorMode);
end;
end;
end;

Not sure what would happen when there are multiple cdroms....but so far..so good..:) Credits go to slick812, its his answer to a similar question.

If someone can adapt this baby to deal with multiple cdroms then won't have to bother for a refund :)

Kind regards,

Paul

Commented:
Here is some function to get the CD drives installed on your system in a StringList:

function GetCDRomPath: TStringList;
var
  i: Integer;
  Drives: DWORD;
  s:string;
  sl:TStringList;
begin
  sl:=TStringList.Create;
  sl.Clear;
  Result := nil;
  Drives := GetLogicalDrives;
  for i := 0 to 25 do
    if ((Drives and (1 shl i)) > 0) and (GetDriveType(PChar(Chr(65 + i) + ':\')) = DRIVE_CDROM) then
      begin
        sl.Add(Chr(65 + i) + ':\');
      end;
      Result:=sl;
end;



Regards

Commented:
Your code adapted here:

function GetCDRomPath: TStringList;
var
  i: Integer;
  Drives: DWORD;
  s:string;
  sl:TStringList;
  MaxLen1,fSysFlags: DWORD;
begin
  sl:=TStringList.Create;
  sl.Clear;
  Result := nil;
  Drives := GetLogicalDrives;
  for i := 0 to 25 do
    if ((Drives and (1 shl i)) > 0) and (GetDriveType(PChar(Chr(65 + i) + ':\')) = DRIVE_CDROM) then
      begin
        sl.Add(Chr(65 + i) + ':\');
  if not GetVolumeInformation(PChar(Chr(65 + i) + ':\'), nil, 0,nil, MaxLen1, fSysFlags,#0,DWORD(sizeof(#0))) then
  begin
  ShowMessage('In station:  '+ PChar(Chr(65 + i) + ':\') + ' is geen cdrom gevonden!');
  end
      end;
      Result:=sl;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.AddStrings(GetCDRomPath);
end;

Commented:
The above code checks to see if there is a CD inserted in any of the CDROMS that are installed in your system

Regards

Commented:
Also another way to put it:
Function to check if a disc is in a drive:

function DiskInDrive(Drive: Char): Boolean;
var
  ErrorMode: Word;
begin
  if not (Drive in ['A'..'Z']) then
    raise EConvertError.Create('Not a valid drive ID');
  ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
  try
    { drive 1 = a, 2 = b, 3 = c, etc. }
    if DiskSize(Ord(Drive) - $40) = -1 then
      Result := False
    else
      Result := True;
  finally
    SetErrorMode(ErrorMode);
  end;
end;


All you have to do is parse through the CDROM drives and ask if there is a disc inside. All this using the 2 functions I posted above (btw the GetVolumeInformation does not work for me... I just used it to use some of your code, but you can get the general ideea by removing the lines that deal with this function)

:-)
Commented:
Simplified:

function DiskInDrive(Drive: Char): Boolean;
var
  ErrorMode: Word;
begin
  ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
  try
    { drive 1 = a, 2 = b, 3 = c, etc. }
    if DiskSize(Ord(Drive) - $40) = -1 then
      Result := False
    else
      Result := True;
  finally
    SetErrorMode(ErrorMode);
  end;
end;

procedure GetCDRomS;
var
  i: Integer;
  Drives: DWORD;
  s:string;
begin
  Drives := GetLogicalDrives;
  for i := 0 to 25 do
    if ((Drives and (1 shl i)) > 0) and (GetDriveType(PChar(Chr(65 + i) + ':\')) = DRIVE_CDROM) then
      begin
        If DiskInDrive(Chr(65 + i)) then ShowMessage('There is a disc inserted in '+Chr(65+i)+':\') else
ShowMessage('There is NO disc inserted in '+Chr(65+i)+':\');

      end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetCDRomS;
end;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hi my friend! :)

First of all tnx for your responses!!

I had justed finished another stage of this project, when I saw your post. This was my attempt to answer my question:

procedure TForm1.btnCBDClick(Sender: TObject);
var i, ErrorMode: integer;
    Drive: String;
    C: Array[0..10] of Char;
    FatType: array [0..MAX_PATH] of Char;
    MaxLen1,fSysFlags: DWORD;
begin
   For i := 1 to 26 do begin
      StrPCopy(C, Chr(I+64)+':\');
      If GetDriveType(C) =  DRIVE_CDROM then
       begin
        FatType[0] := #0;
        Drive := C;
        ErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
if not GetVolumeInformation(PChar(Drive), nil, 0,nil, MaxLen1, fSysFlags,FatType,DWORD(sizeof(FatType))) then
case MessageDlg('In station:  '+ Drive + ' is geen cdrom gevonden! '+#13+#10+'Programma afsluiten? ', mtConfirmation, [mbOK, mbCancel], 0) of
    mrOk:Application.Terminate;
    mrCancel: Break;
  end
   else
  begin
   if FileExists(C + 'I386\_DEFAULT.PI_') then
   PageControl1.ActivePageIndex:=PageControl1.ActivePageIndex +1
  else
  MessageDlg('Dit is geen bruikbare versie van xp!', mtInformation,[mbOK],0) ;
SetErrorMode(ErrorMode);
end;
end;
end;
end;


This one works, it checks for the cdrom drive, it checks whether a disk is inserted and it checks whether some file exists on the disc. That's kinda what I needed, except for the situation in which 2 or more cdrom drives woudl exist ofcourse. I tested your code and it did not workfor me, although the correct cd was inserted it stated there was no cd rom inserted.

So I will stick to the  code I posted here myself...with one remark > my program should be able to cope with the situation in which 2 or more cdrom drives are present...if you manage to adapt my code then all points are yours....I can live with the piece of code I posted...but if you feel like making it more than 1 cd rom drive proof...please do so!

Kind regards,

Paul

Btw I will post some other questions all related to this program...:)

Author

Commented:
Calinutz > I had not noticed your last post, obviously:) I just copied and pasted it, it works great. I'm gonna reward the efforts you put into helping me.

Kind regards,

Paul :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.