title: How to prevent windows from accessing and detecting new volumes while writing a raw image file to PhysicalDrive?
body: <p>After lots and lots of try and error I am now able to flash/write a raw image file (containing multiple partitions) to <code>\\.\PhysicalDriveN</code> (SD-Card in my case) by just using Windows Api calls (<em>see below</em>).</p>
  <p>But as soon as I have some explorer windows open and click around, the write process fails (<em>error 433</em>). Another annoying problem are popup messages telling me that a new volume has to be formatted (at the beginning, while I am still writing!) or autoplay asks me to select an action (after I'm finished).</p>
  <p>How to do it right?</p>
  <p>Here is what I'm doing now (any improvements welcome!):</p>
  <ul>
  <li>for each command, a new Handle is opened and closed, using <a href="https://docs.microsoft.com/en-us/windows/desktop/api/FileAPI/nf-fileapi-createfilea" rel="nofollow noreferrer">CreateFile</a>(&quot;\\.\PhysicalDrive1&quot;, ...) (also tried doing everything with the same Handle, and experimented with various flags like FILE_FLAG_NO_BUFFERING, dwShareMode=0, ...)</li>
  <li><a href="https://docs.microsoft.com/en-us/windows/desktop/api/ioapiset/nf-ioapiset-deviceiocontrol" rel="nofollow noreferrer">DeviceIoControl</a>(Handle, <a href="https://docs.microsoft.com/en-us/windows/desktop/api/WinIoCtl/ni-winioctl-ioctl_disk_delete_drive_layout" rel="nofollow noreferrer">IOCTL_DISK_DELETE_DRIVE_LAYOUT</a>, ...)</li>
  <li><em>Sleep 5s</em></li>
  <li>while(...) <a href="https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile" rel="nofollow noreferrer">WriteFile</a>(Handle, ...)</li>
  <li>DeviceIoControl(Handle, <a href="https://docs.microsoft.com/en-us/windows/desktop/api/WinIoCtl/ni-winioctl-ioctl_disk_update_properties" rel="nofollow noreferrer">IOCTL_DISK_UPDATE_PROPERTIES</a>, ...)</li>
  <li>DeviceIoControl(Handle, <a href="https://docs.microsoft.com/en-us/windows/win32/fileio/ioctl-disk-are-volumes-ready" rel="nofollow noreferrer">IOCTL_DISK_ARE_VOLUMES_READY</a>, ...)</li>
  </ul>
  <p>If I don't touch anything (and ignore the popups), this works most of the time.</p>
  <p><strong>How can I get exclusive access and prevent windows from disturbing me?</strong></p>
  <p>Additional question: how can I know how long to wait after deleting the drive layout? If I start too early, writing fails after some seconds (but some data has already been written).</p>
  <p>This should all run unattended. At the end I need the just flashed partition to be available, so I can add some files. Even though I'm running &quot;are volumes ready&quot;, this doesn't always work immediately and I need multiple retries. Todo: after the files are added, the disk should be <a href="https://stackoverflow.com/questions/3918248/how-to-eject-a-usb-removable-disk-volume-similar-to-the-eject-function-in-win">ejected</a>.</p>
  <p>I tried <em>diskpart / <a href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/automount" rel="nofollow noreferrer">automount disable</a></em>, but this seems not to change anything.</p>
  <p>I tried <em>diskpart / <a href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/offline-disk" rel="nofollow noreferrer">offline disk</a></em>, but it says this isn't supported for removable media.</p>
  <p>I tried various command line tools, like <a href="https://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html#dd-invocation" rel="nofollow noreferrer">dd.exe</a> from the GNU core utils, but they all have the same or even more problems.</p>
  <p><strong>EDIT</strong>
  @DrakeWu-MSFT pointed me to a <a href="https://stackoverflow.com/questions/29170575/how-to-explicitly-lock-a-mounted-file-system">similar question</a> - yes, enumerating the volumes and dismounting them is something I could try, but I would call it a workaround, since the first thing I already do is deleting all volumes. Is that the wrong way? I would prefer to prevent Windows 10 from automatically creating new volumes after I deleted them, so I can just work on physical disks and not logical volumes.</p>
  <p>I analyzed the open source <a href="https://sourceforge.net/projects/win32diskimager/" rel="nofollow noreferrer">Win32 Disk Imager</a> - it doesn't have the problem I have: while it is writing the image, other processes get <em>access denied</em> as it should be. It achieves this by locking and dismounting the volume, which is exactly the accepted solution from above:</p>
  <ul>
  <li>DeviceIoControl(Handle, <a href="https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ni-winioctl-fsctl_lock_volume" rel="nofollow noreferrer">FSCTL_LOCK_VOLUME</a>, ...)</li>
  <li>DeviceIoControl(Handle, <a href="https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ni-winioctl-fsctl_dismount_volume" rel="nofollow noreferrer">FSCTL_DISMOUNT_VOLUME</a>, ...)</li>
  </ul>
  <p>However, what if there is no volume? Then that tool doesn't work at all, it requires a drive letter (and I guess it would fail if there are multiple volumes and drive letters assigned to that disk).</p>
  <p>So, if only volumes can be locked, but not disks, I had to create a dummy volume first, just to be able to get a lock? I don't like that! Still hoping for a better solution...</p>