The magic of creating CD ISO files under Windows

Jan 28, 2005
Last updated: Feb 25, 2005
For comments: tzvetanmi@yahoo.com

Creating an ISO image from a CD should be the most natural thing in the world. Unfortunately under Windows many natural things are not as easy as one might expect (or hope). A quick Google on "create ISO image" reveals many tools for this task, some of them costing about $30 and more. How nice. On other OS-es the following line does the trick

  cat /dev/hda > mycd.iso
but Windows users are not so lucky.

Well, today I had to create an ISO image myself, and not having $30 to spend on crappy software (or the inclination to install anything closed-source on my machine, if it could be avoided), I was forced to overcome my laziness and do something about it. Here is the final solution, a brilliant engineering achievement and probably the best program I have ever written:

  /* This program has been compiled with VC6 and tested under Windows XP SP2 */
  #include <stdio.h>
  #include <share.h>

  int main ( int argc, char ** argv )
  {
    FILE * f, * out;
    char buf[512];
    unsigned len;

    if (argc != 2)
    {
      fprintf( stderr, "Syntax: rdiso filename\n" );
      return 1;
    }

    if ( (f = _fsopen( "\\\\?\\CdRom0", "rb", _SH_DENYNO )) == NULL)
    {
      fprintf( stderr, "Can't open CdRom0\n" );
      return 1;
    }

    if ( (out = fopen( argv[1], "wb" )) == NULL)
    {
      fprintf( stderr, "Can't open %s\n", argv[1] );
      return 1;
    }

    do
    {
      len = fread( buf, 1, sizeof(buf), f );
      if (len)
        fwrite( buf, 1, len, out );
    }
    while (len == sizeof(buf));
    fclose( f );
    fclose( out );
    return 0;
  }

It actually works. I suppose the same technique can be used to make floppy images, etc. It can probably be expanded into a general-purpose devcat application which opens a device file, sets stdout in raw mode and dumps. One has to wonder why such a tool doesn't already exist for Windows. Or perhaps it does, but it costs $30 :-)

BTW, the open source package CDRecord, familiar to most Linux users, does this and much more: it can copy ISO images (readcd), create new ones (mkisofs) or burn them to CDROM (cdrecord). Ross Smith II has compiled it and made it available for Win32/Cygwin here: http://smithii.com/?q=node/view/9.

Actually, he informed me that now even Microsoft are offering ISO burning tools in their Windows Server 2003 Resource Kit Tools. They list the tools Cdburn.exe and Dvdburn.exe. Sadly, they don't provide the very tool which caused the existance of this very page - one that reads a CD into an ISO file !!!

Back to my home page