title: ReadFile doesn't signal EOF at the end of a PhysicalDrive
body: <p>I'm trying to implement a dd equivalent in Windows. [Clarification: I'm trying to replicate the if=/dev/hda of=/dev/hdb functionality of dd, in order to migrate a windows installation to a larger HD. Bizarrely enough, I have used this approach successfully a couple of times now. - G]</p>
  
  <p>The program works (I end up with the source data copied to the destination disk) but does not terminate by itself - I have to tskill it once I'm certain the whole source disk has been read.</p>
  
  <p>I have no problem with opening and locking the source and destination physical drives using the "\\.\PhysicalDriveX" syntax in the CreateFile call.</p>
  
  <p>My problem is in successfully detecting the end of the source drive data.</p>
  
  <p>Currently I'm reading 32K chunks using:</p>
  
  <pre><code>// doesn't work - won't detect End-of-valid drive data and reads endlessly
  while (( success = ReadFile(hInfile, buffer, BUFSIZE, &amp;nRead, NULL )) &amp;&amp; nRead != 0) {
      // ... write the data to the target drive; break if it fails.
      // ... write a progress indicator to the console
  }
  
  // should execute but never does
  if (! success) {
      // an error occurred, do cleanup.
  }
  else {
      // all done, unlock &amp; close filehandles
      puts("ta da!");
  }
  </code></pre>
  
  <p>EOF for a 'normal' file (during synchronous io) is signalled by ReadFile returning TRUE but setting the number of bytes read (nRead) to 0. This is what I attempt in my clumsy while() statement.</p>
  
  <p>ReadFile seems to be returning blocks of null bytes once it has read past the end of the valid data on the source PhysicalDrive.</p>
  
  <p>What's the right way of knowing when to stop reading? - should I use IOCTL_DISK_GET_LENGTH_INFO before I start? It just seems a bit redundant, as ReadFile should report EOF (or fail) on reading past the end of the disk.</p>
  
  <p>Thanks in advance.</p>