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?