This is sort of a follow-up to this question. I want to know if you can access raw devices (i.e. \\\\.\\PhysicalDriveN
) in writing mode and if this should be th
Possibly in Win 7 you have to do something more extreme, such as locking the volume(s) for the disk beforehand with DeviceIoControl(hVol, FSCTL_LOCK_VOLUME, ...)
In Win 7 you don't have to do that; opening and writing with 'rb+' mode works fine.
As eryksun and agf pointed out in the comments (but I didn't really get it at first), the solution is rather simple: you have to open the device in the rb+
mode, which opens the device for updating (as I have found out now..) without trying to replace it with a new file (which wouldn't work because the file is in fact a physical drive).
When writing, you have to write always a whole sector at a time (i.e. multiples of 512-byte), otherwise it fails.
In addition, the .seek()
command can also jump only sector-wise. If you try to seek a position inside a sector (e.g. position 621
), the file object will jump to the beginning of the sector where your requested position is (i.e. to the beginning of the second sector, byte 512
).