swampyankee has asked for the wisdom of the Perl Monks concerning the following question:

I am an unfortunate windows programmer fairly new to the grand world of Perl. My question, and request for knowledge is this: Is there any way, within Perl and either Win32 or Win32API of checking to see if a user has a CD-RW or CD-R drive attached to his/her machine? I don't care if it's kludgy; the only way I can see to do this is to find the CD drive using

while(@drives and (not $is_cdrw))
     {
      my $drive = pop(@drives);
          
      if(GetDriveType($drive) eq DRIVE_CDROM)
          {

     # see if it's writable by trying to write a file....
         $ok = CreateFile($drive + "void");
           }
      }
I am seeking a somewhat less clunky than the above...
emc
  • Comment on How to check to see if a CD drive is a writable on windows?

Replies are listed 'Best First'.
Re: How to check to see if a CD drive is a writable on windows?
by BrowserUk (Patriarch) on Oct 03, 2005 at 20:43 UTC

    Try this. I don't have a writable CD on this system to check what the output would be, but it should be something like CD-R or CD-RW etc. for writable media.

    >c:\windows\system32\wbem\wmic.exe cdrom get mediatype MediaType CD-ROM

    You'll need to look at the options for how to get the information for multiple CD drives.

    You can then chose to just extract the results from running the exe, or you could use the Win32::OLE to access WMI scripting interface, my $locator = Win32::OLE->new ('WbemScripting.SWbemLocator');.

    See Win32::Process::Info for some example code for doing the latter.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      Thank you!

      wmic cdrom get

      seems to provide the information I need.

      I usually rtfm but I find MS's more difficult to get information from than IBM's shelf-o-books.

      emc
Re: How to check to see if a CD drive is a writable on windows?
by terra incognita (Pilgrim) on Oct 04, 2005 at 16:24 UTC
    You can use Win32API to determine if the drive is writable by using the following code. The operation that you send to "DeviceIoControl" is "IOCTL_DISK_IS_WRITABLE" however there is no information about it so I had to go back to the C++ docs.
    use strict; use warnings; use Win32API::File 0.08 qw( :ALL ); my $sRootPath = "d:/"; if(GetDriveType( $sRootPath ) == 5){ # then this is a CD-ROM my $sPath = "//./D:"; my $uAccess = 0; my $uShare = FILE_SHARE_READ(); my $pSecAttr = []; my $uCreate = OPEN_ALWAYS(); my $uFlags = 0; my $hModel = 0; my $hDisk= CreateFile( $sPath, $uAccess, $uShare, $pSecAttr, $uCre +ate, $uFlags, $hModel) or die "Can't read attributes of $sPath: $^E\ +n"; my $controlcode = IOCTL_DISK_IS_WRITABLE; my $inbuff = []; my $inbuffsz = 0 ; my $outbuff = []; my $outbuffsz = 0; my @bytesret = []; my $overlap = []; if (DeviceIoControl($hDisk,$controlcode,$inbuff,$inbuffsz,$outbuff +,$outbuffsz,@bytesret,$overlap)){ print "Status = device is write enabled\n"; }else{ print "Status = $^E\n"; } }
    I have tested this against a floppy with read/write and read only access set and it works correctly, however since I don't have a RW CDRom I can't test it further.