How do I burn to a physical disk in Windows 7?
I am trying to write to a physical disk (e.g. \\.\PhysicalDrive0 ) in Windows 7.
This question was asked to death, but it was never answered. This is what used to work on Windows XP, but Microsoft intentionally violated Windows Vista. Microsoft gives hints on how to do this, but no one was even able to figure it out.
He worked
In the old days you were allowed to write to a physical disk (as long as you are an administrator). The method for this was even documented in a knowledge base article:
INFO: direct disk access under Win32
To open a physical hard disk for direct disk access (raw I / O) in a Win32-based application, use the device name of the form
\\.\PhysicalDriveN
where N is 0, 1, 2, etc., representing each of the physical disks in the system.
You can open a physical or logical disk using the CreateFile () application programming interface (API) with these device names if you have the appropriate disk access rights (that is, you must be an administrator). You must use the CreateFile () FILE_SHARE_READ and FILE_SHARE_WRITE flags to access the drive.
Everything that changed in Windows Vista when additional security restrictions were added.
How do you write to a physical disk?
Many people, and many answers, confuse many stackoverflow questions:
- writing to a physical disk (e.g.
\\.\PhysicalDrive0 ) and - write to a logical volume (for example,
\\.\C$ )
Microsoft notes limitations for both types of operations :
Block direct entry operations for volumes and disks
Write operations on a DASD volume descriptor (direct access device) will succeed if:
- file system not installed or if
- The sectors that are written to are boot sectors.
- Sectors that are written to store the external space of the file system.
- The file system is implicitly locked, requesting exclusive write access.
- The file system was explicitly locked by sending a lock / unlock request.
- The write request was noted by the kernel-mode driver, which indicates that this check should be bypassed. The flag is called SL_FORCE_DIRECT_WRITE, and it is located in the IrpSp-> flags field. This flag is checked by both the file system and the storage drivers.
In my case, I ask you to write physical, not logical. Microsoft notes a new set of restrictions on writing to a physical disk descriptor:
Write operations on a disk descriptor will succeed if:
- Sectors that are written do not enter the file system.
- The sectors that are written fall into the mounted file system, which is explicitly locked.
- Sectors that are written fall into a file system that is not mounted, or the volume does not have a file system.
- My sectors, written, fall into the file system -> fail
- My written sectors end up in a mounted, unlocked file system -> fail
- My sectors, written, fall into the file system that is mounted, and inside the logical volume with the file system.
Tips on how to make it work revolve around:
- unmount a file system
- file system lock
But the question is how to unmount the file system? How to lock a file system?
What are you doing now?
I can read all physical sectors of the disk; It's not a problem. The problem is when I want to write to the physical sector of the disk.
The current code I have is in pseudo code:
void ZeroSector(Int64 PhysicalSectorNumber) { String diskName := '\\.\PhysicalDrive0'; DWORD desiredAccess := GENERIC_READ or GENERIC_WRITE; //INFO: Direct Drive Access Under Win32 //https://support.microsoft.com/en-us/kb/100027 //says you nedd both DWORD shareMode := FILE_SHARE_READ or FILE_SHARE_WRITE; //Open the physical disk hDisk := CreateFile(diskName, desiredAccess, shareMode, nil, OPEN_EXISTING, 0, 0); if hDisk = INVALID_HANDLE_VALUE RaiseLastWin32Error(); try { Int32 bytesPerPhysicalSector := 4096; //Determined elsewhere using IOCTL_STORAGE_QUERY_PROPERTY+STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR //Setup buffer for what we will be writing Byte[] buffer = new Byte[bytesPerPhysicalSector]; //Calculate the byte offset of where the sector is Int64 byteOffset = PhysicalSectorNumber * bytesPerPhysicalSector; //Seek to that byte offset SetFilePointer(hDisk, byteOffset.Lo, byteOffset.Hi, FILE_BEGIN); //Write the buffer DWORD numberOfBytesWritten; if (!WriteFile(hDisk, buffer, bytesPerPhysicalSector, out numberOfBytesWritten, nil)) RaiseLastWin32Error(); } finally { CloseHandle(hDisk); } }
Amazing:
How to do what Microsoft says
Microsoft said that my record failed, and they were right. They said that I need to explicitly lock the file system:
Write operations on a disk descriptor will succeed if:
- The sectors that are written fall into the mounted file system, which is explicitly locked.
In addition, I do not know how to do this.
I know that I probably need to use DeviceIoControl and one of IOCTLS to “lock” the volume. But this creates three problems:
- determination of volume (s) on the selected physical disk
- figure out which IOCTL to use
- figure out how to unlock locked volumes
Ignoring these issues, I blindly tried the LockFile API. Before calling WriteFile :
//Try to lock the physical sector we will be writing if (!LockFile(DiskHandle, byteOffset.Lo, byteOffset.Hi, bytesPerPhysicalSector, 0) RaiseLastWin32Error();
Failure:
ERROR_INVALID_FUNCTION (1)