Re: cheking whether a media is in a slot.

From: EMonk (emonk_at_slingshot.no.uce)
Date: 09/15/04


Date: Wed, 15 Sep 2004 11:43:20 +1200

Doublehp wrote:

> I am still working on CD drives under windows; I d need a trick to
> check whether a media is still present in a drive. I can not try to
> open file in the disk nor try to list the drive, because other apps
> are expected to do so at the same time; in fact I d need a trck which
> gives the response at once ( within 0.1 s ), even if the lense is
> blocked on a reading acces.
>
> What about using some DeviceIoControl ? is it fast ? does need to
> perform a read acces ? ( if yes, then I dont want to use it )
> what about other solutions ?
>
> for example it would be enough if my app was notified of any 'eject
> event' ... but I dont know how to do that.
>
> I am doing C, but accept multitasking and multithreading. I can NOT
> use mciSendCommand or any command of that familly.

One possibility is to use the GetVolumeInformation() API. If there's a
disk mounted it will return volume name and so on, otherwise it will
fail and GetLastError() will return 21 - drive not ready.

Here's a quick-n-nasty way to test whether a CD-ROM drive has a disk
inserted (assuming your 'D' drive is a CD-ROM):

   if (GetDriveInformation("D:\\", NULL, 0, NULL, NULL, NULL, NULL, 0))
   {
   // non-zero result means success
   }
   else
   {
   // failure
   }

You needn't actually ask for information about the drive to discover
whether there's a disk in or not... the call will fail if there isn't,
and should succeed if there is.

One exception though... if the inserted disk doesn't have a recognized
file system (corrupted disk or similar) the call will fail and
GetLastError() returns 1005 (0x3ED) to tell you about it. If you don't
care about whether or not the disk is readable you might want to do a
further check after failure to make sure that it failed because the
drive isn't ready.

   int IsCDPresent(char const* path)
   {
     if (!GetDriveInformation(path, NULL, 0, NULL, NULL, NULL, NULL, 0))
     {
       if (21 == GetLastError())
         return FALSE;
     }
     return TRUE;
   }

Neither bulletproof nor tested... but it's a starting point. There are
a slew of other errors that you might want to check for as well.

-- 
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"


Relevant Pages

  • Re: Eisner? Down? (20 days later)
    ... just buy 1 modern disk and put it all on one disk and just back it up? ... The question is never WHETHER a disk drive will fail. ... If the SYS$MANGLER is paying attention, he should notice the failure no later than the first work day following the failure. ... I made it a point both to check my error counters and log files and ALSO to visit the computer room at least once a day to look for yellow lights on the drives or failed power supplies, listen for "that loud scraping sound", look for things turned off that should be on, etc. ...
    (comp.os.vms)
  • Re: E2K7 SCC Design: 4A+2P vs Two lots of 2A+P
    ... 'cos CCR requires you to have double the disk for practically no ... the likelihood of a database failure. ... would be to fail over. ...
    (microsoft.public.exchange.design)
  • Re: Shock and Awe
    ... Changing bytes IN an open file is absurd! ... modifying the file on disk, then forcing the app to be paged out. ...
    (comp.sys.mac.system)
  • Re: Disk I/O degraded performance
    ... and I experienced lower disk performance for processes that open file ... flag and reads the files to end. ... further into the disk than the other then start reading). ... you might want to check that you are comparing apples to apples; ...
    (Linux-Kernel)
  • Re: How clean is the snapshot when dismounting a member of a shadow set?
    ... > actual on-disk state of an open file is never known for sure. ... when I replace the disk I remove with ... successful upgrade when I have a valid shadow set, ... use it for the system disk on another machine, ...
    (comp.os.vms)