How to write FileDrive (Windows 7) without getting ERROR_ACCESS_DENIED?

I am trying to write a test pattern for each sector of a formatted USB drive. There is one logical drive (for example, h :). This volume is FAT-formatted and contains data that must be overwritten. In addition, I want to overwrite the entire physical disk. The program works with elevated user rights.

First I did the following:

// from the drive letter "h:" I get the physical disk number using // IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS => "\\.\PhysicalDrive2" hDevice = ::CreateFile( "\\.\PhysicalDrive2", GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); // get the number of available sectors with IOCTL_DISK_GET_DRIVE_GEOMETRY_EX // => ulNumberOfSectors // now I try to write some sectors, eg 2 (I want to use a higher value): WriteFile( hDevice, abBuffer, 2*512, &byteswritten, NULL ); 

The WriteFile call does not complete with ERROR_ACCESS_DENIED .

If I write one sector, it works.

When I overwrite the first sector and connect the device again, Windows wants to format it. In this situation, my code with 2048 sectors immediately works without ERROR_ACCESS_DENIED .

I also disabled the volume as described in CodeProject: WriteFile on physical disks with Windows 7 , but this did not change anything. Obviously, the volume is offline because it no longer appears in Windows Explorer.

I want to write more than one sector due to performance reasons. I also fear that other problems may arise in this area, because I do not fully understand the problem.

Any suggestions?

+7
source share
3 answers

I had no problems with different sizes of WriteFile() , but I decided

WriteFile (): access denied <ERROR_ACCESS_DENIED/5> to "\. \ PhysicalDriveX

(usually USB HDD / SSD) in Windows 7 with administrator rights (elevated rights):

Computer Management โ†’ Disk Management:

  • Volume (H: in your case) -> right click -> Delete volume
  • Drive (drive 2 in your case) โ†’ right click โ†’ Off-line
  • Drive (drive 2 in your case) -> right-click -> on-line

After that, I can write to \\\\\\\\\\\\\ without problems

I think Win7 blocks (unlike previous versions of Windows) a physical device if there is any file system on the device to avoid consistency issues.

+3
source

You cannot directly access disk sectors that belong to a mounted file system.

See Changes to the file system and storage stack to restrict direct access to the disk and direct access to the volume.

The documentation for FSCTL_DISMOUNT_VOLUME describes the following sequence for overwriting a file system:

  • Turn up the volume.
  • Lock the volume.
  • Format the volume.
  • Turn down the volume.
  • Unlock the volume.
  • Close the volume descriptor.

Your template drawing operation will be performed in step 3 instead of formatting.

+2
source

Another method is to use clean to delete all partitions on the disk:

 C:\> diskpart Diskpart> list disk Diskpart> select disk N (where N is your disk number) Diskpart> clean Diskpart> exit 
+1
source

All Articles