• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Is CD In Drive?

I have Delphi 7 and Windows XP. I searched around of the question and basicy found nothing that worked. The stuff listed here: http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20273665.html does not want to work on Windows XP. It doesn't detect anything, wheather I insert or eject the disk. I have 2 CD-Roms, maybe that's why, but I just have no idea on how to get this thing to work. The stuff I found @ torry requires stuff delphi 7 does not have Desginf, etc.

Please help
0
the_modder
Asked:
the_modder
  • 21
  • 10
  • 8
  • +1
1 Solution
 
TAZICommented:
Hi

I tried this in Windows 2000 ... Delphi 5

function DiskInDrive(Drive: Char): Boolean;
var
  ErrorMode: Word;
begin
  { make it upper case }
  if Drive in ['a'..'z'] then Dec(Drive, $20);
  { make sure it's a letter }
  if not (Drive in ['A'..'Z']) then
    raise EConvertError.Create('Not a valid drive ID');
  { turn off critical errors }
  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
    { Restore old error mode }
    SetErrorMode(ErrorMode);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if DiskInDrive('D') = False then
     ShowMessage('Drive not ready')
  else
     ShowMessage('Drive Ready');
 end;

Hope this information is useful

Regards
TAZI
0
 
TAZICommented:
Hi

Also, you can detect the number of CD Rom Drives on the machine and the drive letter associated with that drive.

Herewith, the code that can detect that information.

function GetFirstCDROMDrive: char;
var
  drivemap, mask: DWORD;
  i: integer;
  root: string;
begin
  Result := #0;
  root := 'A:\';
  drivemap := GetLogicalDrives;
  mask := 1;
  for i := 1 to 32 do
  begin
    if (mask and drivemap) <> 0 then
      if GetDriveType(PChar(root)) = DRIVE_CDROM then
      begin
        Result := root[1];
        Break;
      end;
    mask := mask shl 1;
    Inc(root[1]);
  end;
end;


{********************************}

function GetNumberOfCDDrives: Byte;
var
  drivemap, mask: DWORD;
  i: integer;
  root: string;
begin
  Result := 0;
  root := 'A:\';
  drivemap := GetLogicalDrives;
  mask := 1;
  for i := 1 to 32 do
  begin
    if (mask and drivemap) <> 0 then
      if GetDriveType(PChar(root)) = DRIVE_CDROM then
      begin
        Inc(Result);
      end;
    mask := mask shl 1;
    Inc(root[1]);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //** NO OF CD ROM DRIVES ON MACHINE **/
  Label1.Caption := IntToStr(GetNumberOfCDDrives);
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  //** DRIVE LETTER **/
  ShowMessage(GetFirstCDROMDrive);
end;
end.

Regards
TAZI
0
 
the_modderAuthor Commented:
Read this link: http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20273665.html . I want something that uses WMDEVICECHANGE, so I can detect when the disk is being READ! That's what I want to do. For some reason I can't get the code to work. I put it in a form, and I play around with both of my cd-drives and no messeges come up!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
the_modderAuthor Commented:
Like I can't get this: http://www.torry.net/vcl/filedrv/cd/cdevents.zip to work in delphi 7 and under XP. I can't even load it into a form without getting errors like DsgnIntF when I include CDEvents in the uses of Form1.

Can anyone help? Basicly what I want to do is contained in the CDEvents file. I want to get it to work.
0
 
esoftbgCommented:
Hi,
You can download a programm DISKSN.exe from which is developed by Delphi 6.0 on Windows XP:
http://eo-soft.addr.com/
0
 
the_modderAuthor Commented:
No, I do not need the serial number of the disk. I need to find out when the disk is being READ! I don't need to know when the disk is recognized by explorer, cuz I already know how to do that =)
0
 
esoftbgCommented:
The question is: "Is CD In Drive?"
When the drive do not contains CD or the tray is opened you will see serial number = 0,
When the drive contains CD and the tray is closed you will see the CD's serial number.
If you need to find out when the disk is being READ, just ask question: "is the disk being READ?"
0
 
the_modderAuthor Commented:
Ok, fine I messed up on the title. So?
0
 
the_modderAuthor Commented:
ANYONE??
0
 
ginsonicCommented:
Try to hook this:

Private
    { Private declarations }
    Procedure WMDeviceChange(Var Msg: TMessage); message WM_DEVICECHANGE;


Procedure TForm1.WMDeviceChange(Var Msg: TMessage);
Const
  CD_IN = $8000;
  CD_OUT = $8004;
Begin
  Inherited;
  Case Msg.wParam Of
    CD_IN   : ShowMessage('CD in');
    CD_OUT  : ShowMessage('CD out')
  End
End;
0
 
the_modderAuthor Commented:
Sorry ginsonic =(

The code doesn't do anything. I just get a blank form and when I eject or inject either of my 2 cd-rom drives nothing comes up =(

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    Procedure WMDeviceChange(Var Msg: TMessage); message WM_DEVICECHANGE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Procedure TForm1.WMDeviceChange(Var Msg: TMessage);
Const
 CD_IN = $8000;
 CD_OUT = $8004;
Begin
 Inherited;
 Case Msg.wParam Of
   CD_IN   : ShowMessage('CD in');
   CD_OUT  : ShowMessage('CD out')
 End
End;

end.
0
 
esoftbgCommented:
Hi,
try the last version of DISKSN.exe from:
http://eo-soft.addr.com/
it is the best I can develope for now !!!
0
 
the_modderAuthor Commented:
Yeh, DISKSN works GREAT! Now if you can give me the source I'll give you 50 points =)
0
 
esoftbgCommented:
I just uploaded DISKSN.zip with Delphi 6.0 code of DISKSN.exe
I am glad to help you,
emil
0
 
the_modderAuthor Commented:
So Where's the code?
0
 
the_modderAuthor Commented:
Great!

That's exactly what I needed!

Thanks for the code.
0
 
ginsonicCommented:
I'm interested to take a look to codes, but I think that aren't available on net anymore.

I offer 200 points for these lines. Can I?
0
 
the_modderAuthor Commented:
Ok.

I have finnaly had time to play around with the code.

Just a quick question: how do I detect letter of the disk that was inserted.
0
 
ginsonicCommented:
Where can I get de zip file with the source? I offer points fot this.
0
 
the_modderAuthor Commented:
I have the file on my hard drive. But I do not understand how to detect which(what) disk was inserted.

I will go home and upload the file to my server.
0
 
the_modderAuthor Commented:
Here ginsonic:

http://jimhasskillz.servequake.com/~dburban/DISKSN.zip

So, do you know how to display the letter of the disk that was ejected/injected?
0
 
ginsonicCommented:
the_modder, I added 200 points in 'Points for the_modder'.  About your problem I will take a look to the code.
0
 
the_modderAuthor Commented:
Great! Thanks!

I'll also give you points =)
0
 
esoftbgCommented:
 Excuse me,

  I did stop to read the comments about this question after I got the points from the_modder.
If anybody is interested of this code and offers so fantastic amount of 200 points. I love the points :-)
  I will publish the code again. I think it will be a great deal for me, WOW !!!!

:-)

emil
0
 
the_modderAuthor Commented:
I got the points from ginsonic already =)

Do you have an idea of how to detect the name of the disk that was inserted?
0
 
esoftbgCommented:
 I really have not an idea of how to detect the name of the disk that was inserted or ejected, but I do not stop to search about.
  I believe there is some unpublished (or published but unknown for me)  Microsoft's function that provides this information. It just wait to be found !!!!
  Best regards,
emil
0
 
the_modderAuthor Commented:
I was kind of thinking of making a buffer for the listview. If something is not the same then it will report the thing that is not the same.
0
 
ginsonicCommented:
the_modder please let a comment here to paq the question and to get your points:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20611891.html
0
 
ginsonicCommented:
esoftbg, I will put a question with 200 points for you, too. Thank for sharing :)
0
 
esoftbgCommented:
I will share the last version of DISKSN's code after 10-12 hours. Please wait...
emil
0
 
esoftbgCommented:
please visit:
http://eo-soft.150m.com/
for the last version of disksn's Delphi 7 source code

emil
0
 
the_modderAuthor Commented:
Hey, ginsonic: I just notice you said you'd give me 200 points =)

Anyway I don't really care =)
0
 
ginsonicCommented:
My pleasure :)
0
 
the_modderAuthor Commented:
Here's how I got my computer to tell me if E is ejected or inserted:

procedure TFormDevices.WMDEVICECHANGE(var msg:tmessage);
const
  DBT_DEVICEARRIVAL = $8000;
  DBT_DEVICEMOVECOMPLETE = $8004;
var
  S:      string;
  temp : string;
  temp1 : string;
begin
  inherited;
  S := '';
  //listview1.Items.Item[b].SubItems[3]
  temp := lv.Items.item[1].SubItems[2];
  edit1.Text := temp;
  try
    case msg.WParam of
      DBT_DEVICEARRIVAL:      S := 'CD disk was Inserted';
      DBT_DEVICEMOVECOMPLETE: S := 'CD tray was Ejected';
    end;
  finally
  Check_Devices(false);
  temp1 := lv.Items.item[1].SubItems[2];
  if temp1 = temp then
  else
  if temp = '00000000' then
  showmessage('F Inserted')
  else
  showmessage('F Ejected');

    EditCD.Text := S;
//    ShowMessage(S);
  end;
end;
0
 
the_modderAuthor Commented:
Ok. I was playing around with the code yesturday and I almost figured out how to get the right disk, and whether it is ejected or inserted =)
0
 
ginsonicCommented:
May we see the final solution :)
0
 
the_modderAuthor Commented:
Today I worked on it some more.

It can now tell which drive the cd was INJECTED into.

I am still working on the ejection part =)

I'll post the updated code when it's finished, but I did remove the create listview (cuz i could not acess some features in it).
0
 
the_modderAuthor Commented:
Ok. I FINISHED IT!!!!!

Here's the link: http://jimhasskillz.servequake.com/~dburban/DISKSN80.zip

BTW: I tried to minimize the ammount of strings I use, so that's why I have so much code =) And yes, there are some varibale I made that do not need to be there =|

Have fun!

Hope to see some improvements.
0
 
ginsonicCommented:
thanks for sharing :)
0
 
the_modderAuthor Commented:
No problem.

Does it work on your computer??

You can clean up the code a bit if you like and send it to me =)

Hope you like what I did to it =)

This week I will intergrate it into the app that I needed it to be in =)))
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 21
  • 10
  • 8
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now